无法从CodeIgniter HMVC中的库加载模块

时间:2015-01-06 08:58:36

标签: php 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_login_box \ controllers \ sidebar_login_box.php中的非对象上调用成员函数load_content()

未定义的“load_content()”函数将在下面进一步解释(在侧栏控制器中)。

错误行

MX /装载机

/*Line 140*/if (isset($this->_ci_classes[$class]) AND $_alias = $this->_ci_classes[$class])
/*Line 141*/        return CI::$APP->$_alias;

MX /控制器

/*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"];
        }
    }
}

侧边栏控制器

这就是侧边栏控制器的样子:

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);
}

}

Fire Controller

Fire_Controller是我的核心控制器。我的核心类的前缀是Fire_而不是MY _。

这就是火控制器的样子:

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日的最新稳定版本。

感谢您的时间,

最好的问候。

2 个答案:

答案 0 :(得分:1)

<强>固定。

侧边栏是从set_defaults()方法加载的,该方法由模板库中的构造函数方法调用。由于它没有完全加载,模板对象没有保存在CI的超级对象中,因此无法访问并将错误丢给侧栏模块。

我已经将set_defaults()调用移动到我的模板库的render_page()函数(由模块的控制器调用),现在它的工作正常。

太糟糕了,我在找到解决方案前几个小时加了赏金,呵呵。

答案 1 :(得分:0)

您需要先加载库,然后才能在侧边栏控制器中使用它。它不是从父母传递的。试试这个:

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);
    }
}

干杯!

相关问题