CodeIgniter 2:如何多次扩展CI_Controller?
我已经通过创建一个 MY_Controller.php 成功扩展了 CI_Controller 类,并将其放置在 application/core 目录中。
core/My_Controller.php 看起来像这样:
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
}
}
然后,当我创建普通控制器时,它们看起来像这样:
class Home extends MY_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->view('home');
}
}
我正在创建一个管理后端,我想要一个用于扩展控制器的不同基类而不是 My_Controller。这样我就可以拥有管理控制器的通用方法(即authentication_check等)。
我无法解决的是如何创建另一个扩展CI_Controller的控制器。
管理控制器的目标是扩展与前端控制器不同的基类。
管理基础控制器看起来像这样:
class MY_Admin_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
}
}
管理页面的普通控制器:
class Admin_home extends MY_Admin_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->view('admin_home');
}
}
问题是要扩展 CI_Controller 类,您必须将控制器文件命名为 PREFIX_Controller.php 并放置它位于 core/ 目录中。但我想要两个控制器类,并且它们不能具有相同的文件名。
I have successfully extended the CI_Controller class by creating a MY_Controller.php which I have placed in the application/core directory.
core/My_Controller.php looks something like this:
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.)
What I can't work out is how I create another controller that extends 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你只需将它们放在同一个文件中,我有一个与此完全相同的项目。
我们只在 MY_Controller.php 文件中包含管理和普通扩展控制器,工作正常。
MY_Controller 或其他扩展文件的主要原因是,当您加载基本文件(无论是库、帮助程序等)时,CodeIgniter 会自动启动它们,您可以在这些文件中拥有许多类。
编辑:
您甚至不需要将它们称为
MY_Admin_Controller
或MY_Controller
,我们有Admin_Controller
和User_Controller
和 <MY_Controller
文件中的 code>Ajax_ControllerYou 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.php
file, works fine.The main reason for the
MY_Controller
or 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.Edit:
You don't even need to call them
MY_Admin_Controller
orMY_Controller
, we haveAdmin_Controller
andUser_Controller
andAjax_Controller
in theMY_Controller
File你正在做的事情是正确的。您只需要
application/core
目录中的所有这些文件。这是 Phil Sturgeon 就此发表的一篇文章:http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRYhttp://philsturgeon.uk/blog/2010 /02/CodeIgniter-Base-Classes-Keeping-it-DRY/
技巧是使用
__autoload()
函数 - Phil在他的帖子中描述。What you're doing is correct. You just need all of these files in the
application/core
directory. Here's a post by Phil Sturgeon regarding just this:http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRYhttp://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.这很容易。执行以下操作:
your_ci_app/application/core/
并创建一个名为MY_Controller.php
的 php 文件(此文件将是您的顶级父类所在的位置)驻留)打开您刚刚创建的文件并添加多个类,如下所示:
在此目录下创建您的子控制器
your_ci_app/application/controllers/
。我将其命名为adminchild.php
打开
adminchild.php
并创建控制器代码,确保扩展父类的名称,如下所示:This is pretty easy. Do the following:
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:
Create your children controllers under this directory
your_ci_app/application/controllers/
. I will call itadminchild.php
Open
adminchild.php
and create your controller code, make sure to extend the name of the parent class, like so:如果您想扩展另一个类而不是 CI_controller,则必须包含目标类。例如
if you want to extend another class instead of CI_controller you must include the target class. for example
application/core 文件夹中的所有文件
MY is subclass CI
MY have 2 subclasses Public and Dashboard
公共
仪表板有 2 个子类,Admin 和 User
Admin
User
config/config.php中
在 controller/Home.php 的
All files in the folder application/core
MY is subclass CI
MY have 2 subclasses Public and Dashboard
Public
Dashboard have 2 subclasses, Admin and User
Admin
User
in config/config.php
in controller/Home.php