Home  >  Article  >  Backend Development  >  Codeigniter控制器controller继承问题实例分析_php实例

Codeigniter控制器controller继承问题实例分析_php实例

WBOY
WBOYOriginal
2016-06-07 17:09:38646browse

本文实例讲述了Codeigniter控制器controller继承问题。分享给大家供大家参考,具体如下:

在项目中经常用到这样一种情况,后台中每个页面都要判断Session来确定用户是否登陆状态.对于在Codeigniter中,那么就会考虑每个控制器继承一个公用控制器。

比如:AdminBase 为应用后台的公用的控制器,在每一个应用后台控制器里面都来继承公共的AdminBase ,但是同时要确保AdminBase 也是继承CI_Controller的。

前台HomeBase也是同样的道理。

具体实现很简单,只要在application/core下面新建MY_Controller.php,如下
(MY_是可配置的,application/config/config.php 文件并找到这一项:$config['subclass_prefix'] = 'MY_';)

class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
}
}
class AdminBase extends MY_Controller
{
function __construct()
{
parent::__construct();
......
}
......
}
class HomeBase extends MY_Controller
{
function __construct()
{
parent::__construct();
......
}
......
}

然后在application/controllers里面的控制器就可以继承了,比如application/controllers/admin/blog.php中

class Blog extends AdminBase
{
function __construct()
{
parent::__construct();
......
}
......
}

更多关于CodeIgniter框架相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》

希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn