我正在使用CodeIgniter的应用程序包功能在多个应用程序上加载助手、配置和库。请参阅https://www.codeigniter.com/user_guide/libraries/loader.html
现在,文档建议软件包中可以包含以下文件夹:
config/ helpers/ language/ libraries/ models/
我还有一个控制器,我希望在多个应用程序之间共享它,称为MY_Controller,所有应用程序的基本控制器都将从它继承。但是,在控制器文件夹中添加似乎不起作用。如何在多个应用程序之间共享一个控制器?
发布于 2013-02-01 08:55:31
实际上,我设法很简单地解决了这个问题。仍然不像正确的CI功能那样理想,但基本上我在每个应用程序的/core文件夹中创建了MY_Controller.php文件,其中包含以下内容:
<?php
/**
* Load the global shared MY_Controller
*/
require_once '/../../shared/core/MY_Controller.php';这样做的好处是我不需要修改CI提供的任何核心文件,包括index.php,唯一的缺点是如果我必须(永远)重命名或移动/shared应用程序,我将不得不使用新路径更新每个MY_Controller。
发布于 2013-02-01 00:39:57
您可能可以使用自己的__autoload()函数Phil Sturgeon covers a similar thing in this post来处理此问题。
例如,以下代码的工作方式与预期一致:
index.php
<?php
/**
* Look for any non-CI class in the libraries folder
* @param type $class
*/
function __autoload($class)
{
if(strpos($class, 'CI_') !== 0)
{
@include_once( APPPATH . 'libraries/'. $class . EXT );
}
}
[Rest of index.php as normal]libraries/MY_Controller.php
<?php
class MY_Controller extends CI_Controller {
function __construct() {
parent::__construct();
echo 'my_controller';
}
}controllers/nickbarrett.php
<?php
class nickbarrett extends MY_Controller {
function __construct() {
parent::__construct();
echo 'extending my_controller';
}
public function index() {
echo 'index';
}
}这意味着你不需要在系统文件夹中放任何东西。可能还有几个地方可以放置__autoload()函数,但index.php似乎是最简单的。
注意:我还没有在应用程序包场景中尝试过这一点,所以如果编辑每个应用程序的index.php对您来说可能不可行,我很感激。
https://stackoverflow.com/questions/14617773
复制相似问题