CodeIgniter 2:如何多次扩展CI_Controller?

发布于 2024-12-07 09:43:33 字数 1283 浏览 1 评论 0原文

我已经通过创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

留一抹残留的笑 2024-12-14 09:43:33

你只需将它们放在同一个文件中,我有一个与此完全相同的项目。

我们只在 MY_Controller.php 文件中包含管理和普通扩展控制器,工作正常。

MY_Controller 或其他扩展文件的主要原因是,当您加载基本文件(无论是库、帮助程序等)时,CodeIgniter 会自动启动它们,您可以在这些文件中拥有许多类。

编辑:

您甚至不需要将它们称为 MY_Admin_ControllerMY_Controller,我们有 Admin_ControllerUser_Controller 和 < MY_Controller 文件中的 code>Ajax_Controller

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

瑕疵 2024-12-14 09:43:33

你正在做的事情是正确的。您只需要 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/

技巧是使用 __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-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.

请别遗忘我 2024-12-14 09:43:33

这很容易。执行以下操作:

  1. 转到以下目录: your_ci_app/application/core/ 并创建一个名为 MY_Controller.php 的 php 文件(此文件将是您的顶级父类所在的位置)驻留)
  2. 打开您刚刚创建的文件并添加多个类,如下所示:

    class Admin_Parent 扩展 CI_Controller {
        公共函数 __construct() {
            父级::__construct();
        }
    
        公共函数测试(){
            var_dump("来自 Admin_Parent");
        }
    }
    
    类 User_Parent 扩展 CI_Controller {
    
        公共函数 __construct() {
            父级::__construct();
        }
    
        公共函数测试(){
            var_dump("来自 User_Parent");
        }
    
    }
    
  3. 在此目录下创建您的子控制器 your_ci_app/application/controllers/ 。我将其命名为 adminchild.php

  4. 打开 adminchild.php 并创建控制器代码,确保扩展父类的名称,如下所示:

    class Adminchild 扩展 Admin_Parent {
    
        函数 __construct() {
            父级::__construct();
        }
    
        函数测试() {
            父级::测试();
        }
    
    }
    

This is pretty easy. Do the following:

  1. Go to the following directory: your_ci_app/application/core/ and create a php file called MY_Controller.php (this file will be where your top parent classes will reside)
  2. 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");
        }
    
    }
    
  3. Create your children controllers under this directory your_ci_app/application/controllers/ . I will call it adminchild.php

  4. Open adminchild.php and 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();
        }
    
    }
    
胡渣熟男 2024-12-14 09:43:33

如果您想扩展另一个类而不是 CI_controller,则必须包含目标类。例如

include 'auth.php';

class test extends Auth

if you want to extend another class instead of CI_controller you must include the target class. for example

include 'auth.php';

class test extends Auth
眼眸里的那抹悲凉 2024-12-14 09:43:33

application/core 文件夹中的所有文件


MY is subclass CI
MY have 2 subclasses Public and Dashboard

class MY_Controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();

        echo "This is " . __CLASS__ . "<br />";
    }
}

公共

class Public_Controller extends My_Controller
{
    public function __construct()
    {
        parent::__construct();

        echo "This is " . __CLASS__ . "<br />";
    }
}

仪表板有 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 />";
    }
}

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');

controller/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');
    }
}

All files in the folder application/core


MY is subclass CI
MY have 2 subclasses Public and 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

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

/* 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

//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');
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文