Codeigniter extending extended MY_Controller

前端 未结 5 677
慢半拍i
慢半拍i 2020-12-09 21:07

I have strictly followed the how-to article by Phil Sturgeon, to extend the base controller. But I get still some errors.

My 3 classes:

// applicatio         


        
5条回答
  •  猫巷女王i
    2020-12-09 21:41

    You need to require the Public Controller in your MY_Controller

    // application/libraries/MY_Controller.php
    class MY_Controller extends Controller{
        public function __construct(){
            parent::__construct();
        }
    }
    
    require(APPPATH.'libraries/Public_Controller.php');
    

    You get the error because Public_Controller was never loaded. Doing this would allow you to extend from Public_Controller

    I like what you are doing because I do that all the time.

    You can do this also in your MY_Controller when you want to create an Admin_Controller

    // application/libraries/MY_Controller.php
    class MY_Controller extends Controller{
        public function __construct(){
            parent::__construct();
        }
    }
    
    require(APPPATH.'libraries/Public_Controller.php'); // contains some logic applicable only to `public` controllers
    require(APPPATH.'libraries/Admin_Controller.php'); // contains some logic applicable only to `admin` controllers
    

提交回复
热议问题