对于仪表板应用程序,我想在我的CI3项目中创建一个小部件组件。我发现了一个类似的问题:How to create a widget system in Codeigniter,但似乎这已经过时或者只是不适用于CI3。甚至与"更多信息的链接"结果是404。
当然,我用谷歌搜索了,但没有找到任何有用的东西。一篇文章是从2013年开始的,其中显示了CI2基础的例子。但是,也许这个插件仍然有效,我的设置中有错误。
我放了一个文件" Widget.php"在
/application/third_party/
具有以下内容:
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;
}
}
在我的application/widgets
- 文件夹中,我有一个名为News.php
的文件。
<?php
class News extends Widget
{
function run() {
die('here');
}
}
在application/libraries/
我放置了一个文件 widgetlib.php :
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class widgetlib {
function __construct() {
include(APPPATH . '/third_party/Widget.php');
}
}
最后,在我看来:
<?php widget::run("News"); ?>
然后我在application/config/autoload.php
中检查了自动加载器类:
$autoload['libraries'] =
'session',
'database',
'widgetlib'
);
这导致:
遇到PHP错误
严重性:错误
消息:Class&#39; Modules&#39;找不到
文件名:third_party / Widget.php
行号:20
和
严重性:运行时通知
消息:非静态方法Widget :: run()不应该静态调用,假设来自不兼容的上下文的$ this
文件名:views / index.php