For a dashboard-app, I want to create a widget-component in my CI3-project. I found a similar question: How to create a widget system in Codeigniter, but it seems that this is either outdated or just not usable for CI3. Even the link with the "more information" results in a 404.
I googled that of course, but didn't find anything useful. One post was from 2013, which showed that example von the CI2 base.
However, maybe this plugin still works and I have an error in my setup.
I placed a file "Widget.php" in
/application/third_party/
with the following content:
class Widget
{
public $module_path;
function run($file) {
$args = func_get_args();
$module = '';
/* is module in filename? */
if (($pos = strrpos($file, '/')) !== FALSE) {
$module = substr($file, 0, $pos);
$file = substr($file, $pos + 1);
}
list($path, $file) = Modules::find($file, $module, 'widgets/');
if ($path === FALSE) {
$path = APPPATH.'widgets/';
}
Modules::load_file($file, $path);
$file = ucfirst($file);
$widget = new $file();
$widget->module_path = $path;
return call_user_func_array(array($widget, 'run'), array_slice($args, 1));
}
function render($view, $data = array()) {
extract($data);
include $this->module_path.'views/'.$view.EXT;
}
function load($object) {
$this->$object = load_class(ucfirst($object));
}
function __get($var) {
global $CI;
return $CI->$var;
}
}
In my application/widgets
-folder, I have a file called News.php
.
<?php
class News extends Widget
{
function run() {
die('here');
}
}
In application/libraries/
I placed a file widgetlib.php :
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class widgetlib {
function __construct() {
include(APPPATH . '/third_party/Widget.php');
}
}
and finally, in my view:
<?php widget::run("News"); ?>
Then I checked my autoloader-class in application/config/autoload.php
:
$autoload['libraries'] =
'session',
'database',
'widgetlib'
);
This results in:
A PHP Error was encountered
Severity: Error
Message: Class 'Modules' not found
Filename: third_party/Widget.php
Line Number: 20
and
Severity: Runtime Notice
Message: Non-static method Widget::run() should not be called statically, assuming $this from incompatible context
Filename: views/index.php