欢迎光临
我们一直在努力

CI3如何使用controller继承

这两天看了下CI3,想使用controller继承,后来才发现CI的继承是另有玄机,这里做个笔记。

1、检查application\config\config.php文件
[php]
$config[‘subclass_prefix’] = ‘MY_’;
[/php]
值为My_,那么你的controller需要以这个开头

2、在application\core文件夹下面新建My_Controller.php文件
[php]
<?php
defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);
/**
*My_Controller
**/
class My_Controller extends CI_Controller{
public function __construct(){
parent::__construct();
}
}
?>
[/php]

3、在你的controllers中使用它
[php]
<?php
defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);

class User extends My_Controller{
public function __construct(){
parent::__construct();
$this->load->library(‘theme’);
}
}
?>
[/php]

备注:如果需要扩展多个继承,那么需要在application\core\My_Controller.php文件中书写多个controller
[php]
<?php
defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);
/**
*基础controller
**/
class My_Controller extends CI_Controller{
public function __construct(){
parent::__construct();
}
}

class BaseController extends My_Controller{
public function __construct(){
parent::__construct();
}
}

class AdminController extends My_Controller{
public function __construct(){
parent::__construct();
}
}

?>
[/php]

赞(0) 打赏
未经允许不得转载:程序开发爱好者 » CI3如何使用controller继承
分享到: 更多 (0)

网站设计及开发、微应用及微网站开发、微信公众平台开发、 HTML5手机网站开发、网络整合营销、APP开发、软件开发及企业电商服务

联系我们