CodeIgniter 2: How to extend CI_Controller multiple times?

元气小坏坏 提交于 2019-11-28 06:19:27

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

swatkins

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.

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();
        }
    
    }
    

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

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');
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!