繁体   English   中英

在HMVC Codeigniter模块内部扩展控制器

[英]extend controller inside HMVC Codeigniter module

如何在模块本身内扩展HMVC模块的控制器?

class Backend extends Backend_Controller {
    public function __construct(){
        parent::__construct();
    }
}

假定以下与HMVC相关的典型Codeigniter文件结构:

/
/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”中。

控制器必须在CodeIgniter中扩展CI_Controller。 控制器不能扩展库,但是可以像这样$ this-> load-> library('backendLib');一起包含它们。

如果您使用的是Wiredesignz HMVC扩展,则可以为此使用基本控制器。 只需在核心目录中创建一个backend_controller类,并将其扩展为MX_Controller。 现在,您可以使模块控制器扩展backend_controller。

最好,

巴特

嗨,CI总是寻找以CI_Controller开头的核心类或以MY_开头的扩展类名,例如MY_Controller MY_Email等,如果您要调用的其他任何类都不像库,则可以在config.php中添加以下代码,这将自动加载自定义类

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

来自此Fourm帖子线程的参考http://forum.codeigniter.com/thread-473-post-2679.html#pid2679

现在您可以使用自定义控制器名称Backend_Controller扩展控制器,该类应该在应用程序库或核心目录下可用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM