如何在模块内扩展HMVC模块的控制器?
class Backend extends Backend_Controller {
public function __construct(){
parent::__construct();
}
}
假设以下典型的Codeigniter文件结构与HMVC相关:
/
/application
/application/modules
/application/modules/backen
/application/modules/backen/controllers
/application/modules/backen/controllers/Backend.php
/application/modules/backen/libraries
/application/modules/backen/libraries/Backend_Controller.php
在此结构中获取错误"未找到类"。可以放入文件夹" /application/libraries/Backend_Controller.php"。
答案 0 :(得分:2)
控制器必须在CodeIgniter中扩展CI_Controller。控制器不能扩展库,但它们可以包含它们,如$ this-> load-> library('backendLib');
如果您使用的是Wiredesignz HMVC扩展,则可以使用基本控制器。只需在核心目录中创建一个backend_controller类,并使其扩展MX_Controller。现在,您可以使模块控制器扩展backend_controller。
最佳,
巴特
答案 1 :(得分:0)
你好CI总是寻找核心类开始CI_Controller或扩展类名称以MY_开头像MY_Controller MY_Email等如果你要调用的任何其他类不像库你可以在config.php中添加以下代码将自动加载自定义类< / p>
/*
| -------------------------------------------------------------------------
| Native spl_autoload_register() - by Kenneth Vogt
| -------------------------------------------------------------------------
|
| Here is an updated version of Phil Sturgeon’s code:
|
| Thanks to Phil Sturgeon Kenneth Vogt and InsiteFX.
|
| NOTE:
| Requires PHP 5.3.+
| As of CI 3.0 Dev - The constant EXT has been removed modified
| to use '.php' now instead of EXT.
| should work for all version of CI and PHP 5.3
|
| Place at the bottom of your ./application/config/config.php file.
| -------------------------------------------------------------------------
*/
spl_autoload_register(function($class)
{
if (strpos($class, 'CI_') !== 0)
{
if (file_exists($file = APPPATH . 'core/' . $class . '.php'))
{
include $file;
}
elseif (file_exists($file = APPPATH . 'libraries/' . $class . '.php'))
{
include $file;
}
}
});
来自这个四分之一帖子的参考 http://forum.codeigniter.com/thread-473-post-2679.html#pid2679
现在您可以使用自定义控制器名称Backend_Controller扩展您的控制器此类应该在应用程序库或核心目录下可用