CodeIgniter 2: How to extend CI_Controller multiple times?

前端 未结 5 1072
野性不改
野性不改 2020-12-08 10:38

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_Controlle

5条回答
  •  情歌与酒
    2020-12-08 11:30

    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__ . "
    "; } }

    Public

    class Public_Controller extends My_Controller
    {
        public function __construct()
        {
            parent::__construct();
    
            echo "This is " . __CLASS__ . "
    "; } }

    Dashboard have 2 subclasses, Admin and User

    class Dashboard_Controller extends My_Controller
    {
        public function __construct()
        {
            parent::__construct();
    
            echo "This is " . __CLASS__ . "
    "; } }

    Admin

    class Admin_Controller extends Dashboard_Controller
    {
        public function __construct()
        {
            parent::__construct();
    
            echo "This is " . __CLASS__ . "
    "; } }

    User

    class User_Controller extends Dashboard_Controller
    {
        public function __construct()
        {
            parent::__construct();
    
            echo "This is " . __CLASS__ . "
    "; } }

    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__ . "
    "; //$this->load->view('home'); } }

提交回复
热议问题