代码Igniter 2:如何多次扩展CI_Controller? [英] Code Igniter 2: How to extend CI_Controller multiple times?

查看:200
本文介绍了代码Igniter 2:如何多次扩展CI_Controller?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地扩展了CI_Controller类,通过创建一个MY_Controller.php,我已经放置在application / core目录。



core / My_Controller.php看起来像这样:

  class MY_Controller extends CI_Controller {

function __construct
{
parent :: __ construct();
}
}

类似这样:

  class Home extends MY_Controller {

function __construct b $ b {
parent :: __ construct();
}

function index()
{
$ this-> load-> view('home');
}
}

我正在创建管理后台有一个不同的基类为控制器扩展而不是My_Controller。这是我可以有管理控制器的常用方法(即authentication_check等。)



我不能解决的是如何创建另一个控制器扩展CI_Controller。



目标是让管理控制器扩展与前端控制器不同的基类。



strong>管理基础控制器如下所示:

 类MY_Admin_Controller扩展CI_Controller {

function __construct()
{
parent :: __ construct();
}
}

管理页面的正常控制器: / strong>

  class Admin_home extends MY_Admin_Controller {

function __construct()
{
parent :: __ construct();
}

function index()
{
$ this-> load-> view('admin_home');
}
}

问题是要扩展CI_Controller类命名控制器文件PREFIX_Controller.php并将其放在core /目录中。但我想要两个控制器类,并且它们不能有相同的文件名。

解决方案

我有一个与这完全相同的项目。



我们在 MY_Controller.php 文件中只有管理和常规扩展控制器, p>

MY_Controller 或其他扩展文件的主要原因是CodeIgniter在加载基本文件时自动启动它们



编辑:



你甚至不需要调用 MY_Admin_Controller MY_Controller ,我们有 Admin_Controller < MY_Controller 和 User_Controller Ajax_Controller >文件


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.

解决方案

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.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 or MY_Controller, we have Admin_Controller and User_Controller and Ajax_Controller in the MY_Controller File

这篇关于代码Igniter 2:如何多次扩展CI_Controller?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆