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

查看:30
本文介绍了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,我们有 Admin_ControllerUser_ControllerMY_Controller文件中的Ajax_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天全站免登陆