CodeIgniter 2:如何多次扩展CI_Controller? [英] CodeIgniter 2: How to extend CI_Controller multiple times?

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

问题描述

我已经通过创建放置在application/core目录中的MY_Controller.php成功扩展了CI_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看起来像这样:

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等)

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.)

我无法解决的问题是如何创建另一个扩展CI_Controller的控制器.

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.

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

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/目录中.但是我想要两个控制器类,它们不能具有相同的文件名.

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.

我们在MY_Controller.php文件中只有管理员和普通扩展控制器,都可以正常工作.

We just have both the admin and normal extended controller in the MY_Controller.php file, works fine.

MY_Controller或其他扩展文件的主要原因是,CodeIgniter在加载基本文件(无论是库,助手等)时自动启动它们,在这些文件中可以有许多类.

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.

您甚至不需要将它们称为MY_Admin_ControllerMY_Controller,我们在MY_Controller文件中有Admin_ControllerUser_ControllerAjax_Controller

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

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

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