Yah*_*CEM 8 php codeigniter controllers extending-classes
我正在使用Codeigniter.2.1.3进行网站,所以我需要扩展CI_Controller,这样我就可以添加一个方法来执行所有控制器,所以我做了user_guide中的内容:
在application/core文件夹中创建一个名为MY_Controller.php的文件,在其中创建MY_Controller类扩展CI_Controller,更改我的常规控制器以扩展MY_controller,如下所示:MY_controller.php:
class MY_Controller extends CI_Controller{
protected $page;
# Constructor
function __construct (){
parent::__construct();
#code shared with all controllers
}
public function get_page(){
#code to get_the right page here
}
}
Run Code Online (Sandbox Code Playgroud)
常规控制器名为Regular.php:
class Regular extends MY_Controller{
public function __construct(){
parent::__construct();
}
public function index(){
$this->get_page();
}
}
Run Code Online (Sandbox Code Playgroud)
但是出现以下错误:
致命错误:第2行的/var/www/immo/CodeIgniter_2.1.3/application/controllers/regular.php中找不到类'MY_Controller'
Max*_*rin 12
您需要包含您的MY_Controller课程或自动加载它.我建议你通过在application/config/config.php文件中添加以下内容来自动加载它.
function __autoload($class)
{
if (strpos($class, 'CI_') !== 0)
{
if (file_exists($file = APPPATH . 'core/' . $class . EXT))
{
include $file;
}
}
}
Run Code Online (Sandbox Code Playgroud)