php CodeIgniter 2:如何多次扩展 CI_Controller?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7627587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CodeIgniter 2: How to extend CI_Controller multiple times?
提问by Camsoft
I have successfully extended the CI_Controller class by creating a MY_Controller.php which I have placed in the application/core directory.
我已经通过创建一个 MY_Controller.php 成功地扩展了 CI_Controller 类,并将其放置在 application/core 目录中。
core/My_Controller.php looks something like this:
core/My_Controller.php 看起来像这样:
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
}
}
Then when I create normal controllers they look something like this:
然后当我创建普通控制器时,它们看起来像这样:
class Home extends MY_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->view('home');
}
}
I'm creating a admin back end and I want to have a different base class for controllers to extend instead of My_Controller. This is so I can have common methods for the admin controllers (i.e. authentication_check etc.)
我正在创建一个管理后端,我希望有一个不同的基类来扩展控制器而不是 My_Controller。这样我就可以拥有管理控制器的通用方法(即 authentication_check 等)
What I can't work out is how I create another controller that extends CI_Controller.
我无法解决的是如何创建另一个扩展 CI_Controller 的控制器。
The goal is for admin controllers to extend a different base class than the front-end controllers.
目标是让管理控制器扩展与前端控制器不同的基类。
The admin base controller would look like this:
管理基本控制器如下所示:
class MY_Admin_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
}
}
An normal controller for admin pages:
管理页面的普通控制器:
class Admin_home extends MY_Admin_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->view('admin_home');
}
}
The problem is that to extend the CI_Controller class you must name your controller file PREFIX_Controller.php and place it in the core/ directory. But I want two controller classes and they can't have the same filename.
问题是要扩展 CI_Controller 类,您必须将控制器文件命名为 PREFIX_Controller.php 并将其放在 core/ 目录中。但是我想要两个控制器类并且它们不能具有相同的文件名。
回答by Cubed Eye
You just put both in the same file, I have a project that is exactly the same as this.
您只需将两者放在同一个文件中,我有一个与此完全相同的项目。
We just have both the admin and normal extended controller in the MY_Controller.phpfile, works fine.
我们在MY_Controller.php文件中只有管理员和普通扩展控制器,工作正常。
The main reason for the MY_Controlleror other extended files is so that CodeIgniter auto initiates them when you load the base file (whether library, helper, etc.), you can have many classes in these files.
MY_Controller或其他扩展文件的主要原因是当您加载基本文件(无论是库、帮助程序等)时,CodeIgniter 会自动启动它们,您可以在这些文件中有许多类。
Edit:
编辑:
You don't even need to call them MY_Admin_Controlleror MY_Controller, we have Admin_Controllerand User_Controllerand Ajax_Controllerin the MY_ControllerFile
你甚至不需要调用它们MY_Admin_Controlleror MY_Controller,我们在文件中有Admin_ControllerandUser_Controller和 andAjax_ControllerMY_Controller
回答by swatkins
What you're doing is correct. You just need all of these files in the application/coredirectory. Here's a post by Phil Sturgeon regarding just this:
你在做什么是正确的。您只需要目录中的所有这些文件application/core。下面是 Phil Sturgeon 的一篇关于这个的帖子:
http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY
http://philsturgeon.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY/
http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY
http://philsturgeon.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY/
The trick is to use the __autoload()function - which Phil describes in his post.
诀窍是使用__autoload()函数——Phil 在他的帖子中描述了这一点。
回答by CodeGodie
This is pretty easy. Do the following:
这很容易。请执行下列操作:
- Go to the following directory:
your_ci_app/application/core/and create a php file calledMY_Controller.php(this file will be where your top parent classes will reside) Open this the file you just created and add your multiple classes, like so:
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/. I will call itadminchild.phpOpen
adminchild.phpand create your controller code, make sure to extend the name of the parent class, like so:class Adminchild extends Admin_Parent { function __construct() { parent::__construct(); } function test() { parent::test(); } }
- 转到以下目录:
your_ci_app/application/core/并创建一个名为的 php 文件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"); } }在此目录下创建您的子控制器
your_ci_app/application/controllers/。我会叫它adminchild.php打开
adminchild.php并创建您的控制器代码,确保扩展父类的名称,如下所示:class Adminchild extends Admin_Parent { function __construct() { parent::__construct(); } function test() { parent::test(); } }
回答by Saman Salehi
if you want to extend another class instead of CI_controller you must include the target class. for example
如果要扩展另一个类而不是 CI_controller,则必须包含目标类。例如
include 'auth.php';
class test extends Auth
回答by antelove
All files in the folder application/core
文件夹application/core中的所有文件
MY 是 CI 的子类
MY 有 2 个子类 Public 和 Dashboard
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
echo "This is " . __CLASS__ . "<br />";
}
}
Public
民众
class Public_Controller extends My_Controller
{
public function __construct()
{
parent::__construct();
echo "This is " . __CLASS__ . "<br />";
}
}
Dashboard have 2 subclasses, Admin and User
Dashboard 有 2 个子类,Admin 和 User
class Dashboard_Controller extends My_Controller
{
public function __construct()
{
parent::__construct();
echo "This is " . __CLASS__ . "<br />";
}
}
Admin
行政
class Admin_Controller extends Dashboard_Controller
{
public function __construct()
{
parent::__construct();
echo "This is " . __CLASS__ . "<br />";
}
}
User
用户
class User_Controller extends Dashboard_Controller
{
public function __construct()
{
parent::__construct();
echo "This is " . __CLASS__ . "<br />";
}
}
in config/config.php
在config/config.php
/* load class in core folder */
function my_load($class) {
if (strpos($class, 'CI_') !== 0) {
if (is_readable(APPPATH . 'core' . DIRECTORY_SEPARATOR . $class . '.php' )) {
require_once (APPPATH . 'core' . DIRECTORY_SEPARATOR . $class . '.php');
}
}
}
spl_autoload_register('my_load');
in controller/Home.php
在控制器/Home.php
//class Home extends MY_Controller {
//class Home extends Dashboard_Controller {
class Home extends Admin_Controller {
public function index()
{
echo "This is " . __CLASS__ . "<br />";
//$this->load->view('home');
}
}

