Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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:

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

// application/libraries/Public_Controller.php
class Public_Controller extends MY_Controller{
    public function __construct(){
        parent::__construct();

    }    
}

// application/controllers/user.php
class User extends Public_Controller{
    public function __construct(){
        parent::__construct();
    }
}

Fatal error: Class 'Public_Controller' not found in /srv/www/xxx/application/controllers/user.php on line 2

Curious is that the following snippet is working, if I directly extends from MY_Controller:

// application/controllers/user.php
class User extends MY_Controller{
    public function __construct(){
        parent::__construct();
    }
}

I have loaded the controllers via __autoload() or manually. The controllers are loaded succesfully.

CI-Version: 1.7.3

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
300 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...