Php Can';t从CodeIgniter HMVC中的库加载模块
在CodeIgniter HMVC的模板库中加载模块时遇到问题。我希望在模板库中加载模块的原因是,我希望在模板中为侧框和其他内容框使用模块 PS:我也在使用用于CodeIgniter的Smarty模板解析系统,但我怀疑这与错误有关,但如果您有理由相信其他原因,请告诉我 我想做的事 我尝试以两种不同的方式加载模块,但都出现了相同的错误 错误 遇到一个PHP错误 严重性:通知 消息:未定义的属性CI::$template 文件:MX/Loader.php 行号:141 - 遇到一个PHP错误 严重性:通知 消息:未定义的属性CI::$template 文件名:MX/Controller.php 行号:57 - 致命错误:在第7行的E:\Xampp\htdocs\firecms\application\modules\sidebar\u login\u box\controllers\sidebar\u login\u box.php中的非对象上调用成员函数load\u content() 未定义的“load_content()”函数将进一步解释(在侧栏控制器中) 错误行 MX/加载器Php Can';t从CodeIgniter HMVC中的库加载模块,php,codeigniter,hmvc,Php,Codeigniter,Hmvc,在CodeIgniter HMVC的模板库中加载模块时遇到问题。我希望在模板库中加载模块的原因是,我希望在模板中为侧框和其他内容框使用模块 PS:我也在使用用于CodeIgniter的Smarty模板解析系统,但我怀疑这与错误有关,但如果您有理由相信其他原因,请告诉我 我想做的事 我尝试以两种不同的方式加载模块,但都出现了相同的错误 错误 遇到一个PHP错误 严重性:通知 消息:未定义的属性CI::$template 文件:MX/Loader.php 行号:141 - 遇到一个PHP错误 严重性
/*Line 140*/if (isset($this->_ci_classes[$class]) AND $_alias = $this->_ci_classes[$class])
/*Line 141*/ return CI::$APP->$_alias;
/*Line 56*/public function __get($class) {
/*Line 57*/ return CI::$APP->$class;
MX/控制器
/*Line 140*/if (isset($this->_ci_classes[$class]) AND $_alias = $this->_ci_classes[$class])
/*Line 141*/ return CI::$APP->$_alias;
/*Line 56*/public function __get($class) {
/*Line 57*/ return CI::$APP->$class;
我如何尝试加载模块
这是我第一次尝试(加载文件并实例化其类):
class Template {
//[...]
public function load_sidebars()
{
$sidebars = $this->CI->cms_model->get_sidebars();
foreach ($sidebars as $sidebar)
{
if (trim($sidebar["content"]) == "")
{
//An example of sidebar module name is "sidebar:login_box"
//The function below changes the name to "sidebar_login_box" (the
//module's folder and controller name.
$module = str_replace(':', '_', $sidebar["module"]);
$file_path = APPPATH.'modules/'.$module.'/controllers/'.$module.'.php';
require_once $file_path;
$class = ucfirst($module);
$object = new $class();
$module_data = $object->index();
$this->_section_data["sidebars"][]["content"] = $module_data;
}
else
{
$this->_section_data["sidebars"][]["content"] = $sidebar["content"];
}
}
}
//[...]
}
public function load_sidebars()
{
$sidebars = $this->CI->cms_model->get_sidebars();
foreach ($sidebars as $sidebar)
{
if (trim($sidebar["content"]) == "")
{
$module = str_replace(':', '_', $sidebar["module"]);
$this->CI->load->module($module);
$module_data = $this->CI->$module->index();
$this->_section_data["sidebars"][]["content"] = $module_data;
}
else
{
$this->_section_data["sidebars"][]["content"] = $sidebar["content"];
}
}
}
这是我第二次尝试(使用loader函数):
class Template {
//[...]
public function load_sidebars()
{
$sidebars = $this->CI->cms_model->get_sidebars();
foreach ($sidebars as $sidebar)
{
if (trim($sidebar["content"]) == "")
{
//An example of sidebar module name is "sidebar:login_box"
//The function below changes the name to "sidebar_login_box" (the
//module's folder and controller name.
$module = str_replace(':', '_', $sidebar["module"]);
$file_path = APPPATH.'modules/'.$module.'/controllers/'.$module.'.php';
require_once $file_path;
$class = ucfirst($module);
$object = new $class();
$module_data = $object->index();
$this->_section_data["sidebars"][]["content"] = $module_data;
}
else
{
$this->_section_data["sidebars"][]["content"] = $sidebar["content"];
}
}
}
//[...]
}
public function load_sidebars()
{
$sidebars = $this->CI->cms_model->get_sidebars();
foreach ($sidebars as $sidebar)
{
if (trim($sidebar["content"]) == "")
{
$module = str_replace(':', '_', $sidebar["module"]);
$this->CI->load->module($module);
$module_data = $this->CI->$module->index();
$this->_section_data["sidebars"][]["content"] = $module_data;
}
else
{
$this->_section_data["sidebars"][]["content"] = $sidebar["content"];
}
}
}
侧栏控制器
这是边栏控制器的外观:
class Sidebar_login_box extends Fire_Controller {
public function index()
{
$view_data = array();
//The load_content function in the template library is used to parse template files
//and return them as a string.
return $this->template->load_content("login_box", $view_data);
}
class Fire_Controller extends MX_Controller {
public function __construct()
{
parent::__construct();
//Load configurations from the database.
$this->config->load_db_configs();
//Set the timezone.
date_default_timezone_set(config_item("timezone"));
//Loads the form validation library.
$this->load->library("form_validation");
//Reset the Form Validation CI Object (to fix problems with HMVC CI).
$this->form_validation->CI =& $this;
//To reduce load time, the template library will not be loaded in ajax
//requests.
if ( ! $this->input->is_ajax_request())
{
$this->load->library("template");
}
//Force access via SSL connection (HTTPS) if necessary.
if ((int)config_item('force_https') === 1)
{
force_https();
}
}
}
消防控制器
消防控制器是我的核心控制器。我的核心类前缀是Fire而不是My
这是消防控制器的外观:
class Sidebar_login_box extends Fire_Controller {
public function index()
{
$view_data = array();
//The load_content function in the template library is used to parse template files
//and return them as a string.
return $this->template->load_content("login_box", $view_data);
}
class Fire_Controller extends MX_Controller {
public function __construct()
{
parent::__construct();
//Load configurations from the database.
$this->config->load_db_configs();
//Set the timezone.
date_default_timezone_set(config_item("timezone"));
//Loads the form validation library.
$this->load->library("form_validation");
//Reset the Form Validation CI Object (to fix problems with HMVC CI).
$this->form_validation->CI =& $this;
//To reduce load time, the template library will not be loaded in ajax
//requests.
if ( ! $this->input->is_ajax_request())
{
$this->load->library("template");
}
//Force access via SSL connection (HTTPS) if necessary.
if ((int)config_item('force_https') === 1)
{
force_https();
}
}
注意:这是我最近的一个项目,这意味着框架和所有第三方扩展都是截至2015年1月6日的最新稳定版本
谢谢你抽出时间
致以最诚挚的问候。您需要先加载库,然后才能在侧栏控制器中使用它。它不是从父级传递的。试试这个:
class Sidebar_login_box extends Fire_Controller {
public function index()
{
$view_data = array();
$this->load->library('template');
//The load_content function in the template library is used to parse template files
//and return them as a string.
return $this->template->load_content("login_box", $view_data);
}
}
干杯 已修复。 侧栏是从set_defaults()方法加载的,该方法由模板库中的构造函数方法调用。由于没有完全加载,模板对象没有保存在CI的超级对象中,因此无法访问,并在侧栏模块中抛出错误 我已经将set_defaults()调用移动到模板库的render_page()函数(由模块的控制器调用),现在它工作得非常好
很遗憾,在找到解决方案前的几个小时,我添加了赏金,呵呵。谢谢versalle的回答,但你不理解这个问题。扩展Fire Controller的所有模块都可以毫无问题地访问模板对象,但我从模板库加载的模块除外。如果我尝试在侧栏模块中加载库,错误仍然存在。