[英]How to create two parent controllers in Codeigniter?
I want to create two parent controllers: one for admin and one for user site. 我想创建两个父控制器:一个用于管理员,一个用于用户站点。 They have to extend a regular Controller class but each of them has to do different things.
他们必须扩展常规的Controller类,但是每个人都必须做不同的事情。
I wrote up an article showing how you do this. 我写了一篇文章,说明您如何执行此操作。
http://philsturgeon.co.uk/news/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY http://philsturgeon.co.uk/news/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY
You need to create an __autoload() function in your config.php or directly include the base controller above the class definition. 您需要在config.php中创建__autoload()函数,或直接在类定义上方包含基本控制器。
This is pretty easy. 这很容易。 Do the following:
请执行下列操作:
your_ci_app/application/core/
and create a php file called MY_Controller.php
(this file will be where your top parent classes will reside) your_ci_app/application/core/
并创建一个名为MY_Controller.php
的php文件(此文件将是您顶级父类所在的位置) Open MY_Controller.php
and add your multiple classes, like so: 打开
MY_Controller.php
并添加您的多个类,如下所示:
class Admin_Parent extends CI_Controller { public function __construct() { parent::__construct(); } public function test() { var_dump("from Admin_Parent"); } } class User_Parent extends CI_Controller { public function __construct() { parent::__construct(); } public function test(){ var_dump("from User_Parent"); } }
Create your children controllers under this directory your_ci_app/application/controllers/
. 在此目录
your_ci_app/application/controllers/
下创建子控制器。 I will call it adminchild.php
我将其
adminchild.php
Open adminchild.php
and create your controller code, make sure to extend the name of the parent class, like so: 打开
adminchild.php
并创建您的控制器代码,确保扩展父类的名称,如下所示:
class Adminchild extends Admin_Parent { function __construct() { parent::__construct(); } function test() { parent::test(); } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.