27

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

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

4

5 回答 5

29

你只是把两者放在同一个文件中,我有一个和这个完全相同的项目。

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

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

编辑:

您甚至不需要调用它们MY_Admin_ControllerMY_Controller,我们在文件中有Admin_ControllerandUser_ControllerAjax_ControllerMY_Controller

于 2011-10-02T16:30:12.407 回答
17

你在做什么是正确的。您只需要目录中的所有这些文件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 在他的帖子中描述了该功能。

于 2011-10-03T14:17:02.193 回答
3

这很容易。请执行下列操作:

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

    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. 在此目录下创建您的子控制器your_ci_app/application/controllers/。我会叫它adminchild.php

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

    class Adminchild extends Admin_Parent {
    
        function __construct() {
            parent::__construct();
        }
    
        function test() {
            parent::test();
        }
    
    }
    
于 2015-02-05T17:11:22.833 回答
1

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

include 'auth.php';

class test extends Auth
于 2016-06-18T07:56:59.590 回答
0

文件夹应用程序/核心中的所有文件


我是 CI 的子类
我有 2 个子类 Public 和 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 />";
    }
}

Dashboard 有 2 个子类,Admin 和 User

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

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

行政

class Admin_Controller extends Dashboard_Controller
{
    public function __construct()
    {
        parent::__construct();

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

用户

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

控制器/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');
    }
}
于 2018-08-14T06:44:40.543 回答