Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter extending extended MY_Controller

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

like image 880
toktok Avatar asked Jan 05 '11 20:01

toktok


4 Answers

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
like image 77
Teej Avatar answered Nov 13 '22 20:11

Teej


You should place Public_controller in with MY_Controller inside MY_Controller.php

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

class Public_Controller extends MY_Controller{
    public function __construct(){
        parent::__construct();

    }    
}

I use __construct everywhere and it works fine I recently wrote up an article on how to do this in relation to wrapping your auth logic into your extended controllers. It's about half way down when I start discussing constructing your controllers.

like image 29
jondavidjohn Avatar answered Nov 13 '22 18:11

jondavidjohn


Problem was solved here: http://devcrap.net/pl/2011/09/04/codeigniter-dziedziczenie-z-my_controller-extends-my_controller/. In polish but code is good :]

like image 25
devcrap.net Avatar answered Nov 13 '22 18:11

devcrap.net


I had problem like this,After some search I found error was made myself,Because my controller class name was MY_Controller but file name was My_Controller[Case not matching]. Note:- In localhost I didnt have any error.

In extended controller I Use

class Home  extends MY_Controller{
   function __construct() {
   parent::__construct();
  }
}

even I got the error.

After changing my file name to MY_Controller it started to work well.

like image 1
shihabudheen Avatar answered Nov 13 '22 19:11

shihabudheen