EllisLab text mark
Advanced Search
     
Extending CI_Controller is not working
Posted: 27 February 2011 12:00 PM
Joined: 2011-02-27
8 posts

Hello,

i am using CodeIgniter 2.0.

I have a base controller class ControllerBase (ControllerBase.php) that extends the CI_Controller.
I have put this in the application/libraries folder.

class ControllerBase extends CI_Controller
{
    
var $_layout;

    function 
__construct($layout 'shared/_layout')
    
{
        parent
::__construct();
        
$this->_layout $layout;
    
}

    
function setLayout($layout)
    
{
        $this
->_layout $layout;
    
}

    
function loadLayoutView($view$title ""$data null$return false)
    
{       
        $data[
'title'$title;
        
$data['view'$this->load->view($view$datatrue);

        return 
$this->load->view($this->_layout$data$return);
    
}

The idea is that i usually want this to be the class that all my Controllers extends, and I can put all shared functions in only one place.

But whenever i try to extend this class (ControllerBase) from one of my Controllers (in application/controllers) i get an error saying “Fatal error: Class ‘ControllerBase’ not found in…...”

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

    
function index()
    
{
        $this
->loadLayoutView('home/index''Home');
    
}

I have registered ControllerBase in the autoload.php

$autoload['libraries'= array('ControllerBase'); 

The file is being loaded - if i try to echo out something at the top of the file and i dont try to extend it.

I have also tried naming the file with the MY_ prefix but it doesnt make a difference, I still get the error message when i try to extend the class.

It works fine if I put the ControllerBase class in the same file as the Home class

So i suspect it has something to do with the autoloading happening to late in the proccess (after the Home class tries to extend the ControllerBase), or maybe the naming of the file?

I assume application/libraries is the correct place to put the file?

Anyone who can figure out what i am doing wrong ?

 
Posted: 27 February 2011 02:32 PM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-19
6583 posts

MY_Controller extends CI_Controller - place MY_Controller in application/core

Extended Controllers go in application/controllers

Home extends MY_Controller

Read - Phil Sturgeon’s CodeIgniter Base Classes: Keeping it DRY

InsiteFX

 Signature 

Certified State of CT Computer Programming Teacher.
Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

Skype: insitfx

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View

 
Posted: 27 February 2011 04:03 PM   [ # 2 ]   [ Rating: 0 ]
Joined: 2011-02-27
8 posts

Thanks for the reply.

Works perfectly.
But not quite happy with the file name (MY_Controller) ... but i will live with it. smile

Thanks again.

 
Posted: 27 February 2011 05:41 PM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4489 posts

You can change the default ‘MY_’ prefix to anything you want in the config.

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM <= LOOKING FOR A NEW MAINTAINER!

 
Posted: 20 April 2011 12:05 AM   [ # 4 ]   [ Rating: 0 ]
Joined: 2010-06-16
35 posts
InsiteFX - 27 February 2011 07:32 PM

MY_Controller extends CI_Controller - place MY_Controller in application/core

Extended Controllers go in application/controllers

...

Should this be a listed step in the upgrading documentation?

In 2.01 to 2.02, step 3 mentions that security should be moved…but I don’t see any other steps instructiong about what to do with extended controllers.

 
Posted: 20 April 2011 12:09 AM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-19
6583 posts

You need to add this!

/*
| -------------------------------------------------------------------
|  Native Autoload - by Phil Sturgeon.
| -------------------------------------------------------------------
|
| Nothing to do with config/autoload.php, this allows PHP autoload to work
| for base controllers and some third-party libraries.
|
| If using HMVC you do not need this! HMVC will autoload.
|
| Place this code at the bottom of your application/config/config.php file.
*/
function __autoload($class)
{
    
if (strpos($class'CI_') !== 0)
    
{
        
@include_once(APPPATH 'core/' $class EXT);
    
}

InsiteFX

 Signature 

Certified State of CT Computer Programming Teacher.
Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

Skype: insitfx

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View

 
Posted: 24 August 2011 07:48 AM   [ # 6 ]   [ Rating: 0 ]
Joined: 2011-08-24
3 posts

Hello!

I am usually using CI_Controller as a base controller by extending it for my controller classes. But for some controllers I want to use a different one. So I don’t want to replace it, just to have some alternatives for it in some particular situations like:

class Article_controller extends CI_Controller {
    
// ...

InsiteFX, I added your _autoload function, but CodeIgniter does not autoload my new controller “application/core/CI_Article_controller”. The error appears in a controller that uses it like this:

class Article extends CI_Article_controller {
    
// ...

It says:

Fatal error: Class 'CI_Article_controller' not found in /var/www/devel/application/controllers/article.php on line XX 

I suppose that your __autoload function must be called somewhere, but I can’t figure out where to do this.

Since I created a new Core Class (not extending and not replacing one of CodeIgniter), more generally: how do I autoload my new core classes? CodeIgniter only allows me to extend or replace core classes not to create new ones.

 
Posted: 24 August 2011 08:11 AM   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-19
6583 posts

If you extend a system/core class then it goes in application/core
If you extend a system/libraries class then it goes in application/libraries

Example:
These go in application/core

class Base_Controller extends CI_Controller {

    
/**
     *  __construct
     * 
     * Class Constructor    PHP 5+
     * 
     * @access    public
     * @return    void
     */
     
public function __construct()
     
{
         parent
::__construct();

    
}
}

class Admin_Controller extends Base_Controller {

    
/**
     *  __construct
     * 
     * Class Constructor    PHP 5+
     * 
     * @access    public
     * @return    void
     */
     
public function __construct()
     
{
         parent
::__construct();

    
}
}

class Public_Controller extends Base_Controller {

    
/**
     *  __construct
     * 
     * Class Constructor    PHP 5+
     * 
     * @access    public
     * @return    void
     */
     
public function __construct()
     
{
         parent
::__construct();

    
}

Example:
Phil’s autoloader this goes at the bottom of application/config/config.php

/*
| -------------------------------------------------------------------
|  Native Autoload - by Phil Sturgeon. New Version!
| -------------------------------------------------------------------
|
| Nothing to do with config/autoload.php, this allows PHP autoload to work
| for base controllers and some third-party libraries.
|
| If using HMVC you do not need this! HMVC will autoload.
|
| Place this code at the bottom of your application/config/config.php file.
*/
function __autoload($class)
{
    
if (strpos($class'CI_') !== 0)
    
{
        
if (file_exists($file APPPATH 'core/' $class EXT))
        
{
            
include $file;
        
}
        
elseif (file_exists($file APPPATH 'libraries/' $class EXT))
        
{
            
include $file;
        
}
    }

InsiteFX

 Signature 

Certified State of CT Computer Programming Teacher.
Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

Skype: insitfx

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View

 
Posted: 24 August 2011 08:32 AM   [ # 8 ]   [ Rating: 0 ]
Joined: 2011-08-24
3 posts

I found my mistake. First of all I didn’t know about __autoload PHP5 function. Secondly I had to name my new basic Controller class without a ‘CI_’ prefix: Article_controller instead of CI_Article_controller.

 
Posted: 24 August 2011 08:54 AM   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-19
6583 posts

I do not use Phi;‘s any more, I have written my own new autoloader using SPL_Autoload which will the __autoload if it is used!

InsiteFX

 Signature 

Certified State of CT Computer Programming Teacher.
Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

Skype: insitfx

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View

 
Posted: 25 August 2011 06:02 AM   [ # 10 ]   [ Rating: 0 ]
Joined: 2011-08-24
3 posts

Thank you for your post!

I have written my own new autoloader using SPL_Autoload

What is the advantage of your own autoloader and how does it work (if it’s not a secret wink )?