CodeIgniter Forums
Extending the controller class - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Extending the controller class (/thread-35066.html)



Extending the controller class - El Forum - 10-18-2010

[eluser]richzilla[/eluser]
Hi All,

When extending the controller class, does it have to contain the name of the class that it is extending?

for example MY_Controller contains the name of the controller class that it is extending. The reason i ask is that i am attempting to extend the controller class twice, once for the admin part of my site and once for the normal 'site'. Ive named the new classes MY_Site and MY_Admin and placed them both in the libraries folder, however this does not seem to work, as nothing is generated when i request pages that implement these classes? any help would be appreciated.


Extending the controller class - El Forum - 10-18-2010

[eluser]n0xie[/eluser]
Read this


Extending the controller class - El Forum - 10-19-2010

[eluser]richzilla[/eluser]
thanks for the help, ive tried the suggestions mentioned there, but still no luck. so far i have:

a MY_Controller:

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

A Site_Controller:

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

and an Admin_Controller:

Code:
class Admin_Controller extends MY_Controller {

    function Admin_Controller() {
        parent::MY_Controller();
    }

but still no luck, it should be noted that if simply extend my usual controllers directly from the Controller class, they work without a hitch, so my code is ok. Also on a slightly different note, what is the purpose of this :

Code:
function __autoload($class)
{
if(strpos($class, 'CI_') !== 0)
{
  @include_once( APPPATH . 'libraries/'. $class . EXT );
}
}

Any help would be appreciated.


Extending the controller class - El Forum - 10-19-2010

[eluser]kaejiavo[/eluser]
[quote author="ricardino" date="1287523660"]thanks for the help, ive tried the suggestions mentioned there, but still no luck. so far i have:

a MY_Controller:

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

A Site_Controller:

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

and an Admin_Controller:

Code:
class Admin_Controller extends MY_Controller {

    function Admin_Controller() {
        parent::MY_Controller();
    }

but still no luck, it should be noted that if simply extend my usual controllers directly from the Controller class, they work without a hitch, so my code is ok. Also on a slightly different note, what is the purpose of this :

Code:
function __autoload($class)
{
if(strpos($class, 'CI_') !== 0)
{
  @include_once( APPPATH . 'libraries/'. $class . EXT );
}
}

Any help would be appreciated.[/quote]

Hi,
you have to use only one file: MY_Controller.php as this will be autoloaded by CI.
Then you put all your three classes in this file, like:
Code:
class MY_Controller extends Controller {
    
    function __construct() {
        parent::Controller();
    }
        
}

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

class Admin_Controller extends MY_Controller {

    function Admin_Controller() {
        parent::MY_Controller();
    }

Then you can extend your Site_Controller or Admin_Controller as you like.

Marco


Extending the controller class - El Forum - 10-19-2010

[eluser]richzilla[/eluser]
i put everything into the same file - didnt seem to work, i get absolutely no output to the screen when i do that. Just to clear up any confusion im using CI 1.7.2 if thats an issue?


Extending the controller class - El Forum - 10-19-2010

[eluser]InsiteFX[/eluser]
MY_Controller need to go into application/libraries

If running CodeIgniter 2.0 MY_Controller needs to go into application/core

Code:
// PHP 5
class MY_Controller extends Controller {
    
    function __construct() {
        parent::__construct();
    }

    function index() {

    }        
}

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

    function index() {

    }
}

class Admin_Controller extends MY_Controller {

    function Admin_Controller() {
        parent::__construct();
    }

    function index() {

    }
}

if you do not have an index function and load a view you will get a blank page.

InsiteFX


Extending the controller class - El Forum - 10-19-2010

[eluser]cahva[/eluser]
The purpose of __autoload is that when using PHP5, it will automatically load your classes from the libraries folder if they are the same name. When using this __autoload, you dont actually need MY_Controller at all.

Put the autoload to the config.php as suggested.

Create Admin_Controller.php to libraries folder.
Code:
class Admin_Controller extends Controller {
    function __construct()
    {
        parent::__construct();
        // Do something here eg. check if logged in
        $this->load->library('ion_auth');

        if (!$this->ion_auth->logged_in())
        {
            redirect('login');
        }
    }
}

Create Site_Controller.php to libraries folder.
Code:
class Site_Controller extends Controller {
    function __construct()
    {
        parent::__construct();
        // Do something else here. Maybe load some helpers, Create content that you need sitewide etc..
    }
}

Then in controllers you extend from one of those. Normal pages would use Site_Controller and pages that needs authenticated user extends from Admin_Controller.

controllers/home.php
Code:
class Home extends Site_Controller {

    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $this->load->view('home');
    }

}

controllers/backend.php
Code:
class Backend extends Admin_Controller {

    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $this->load->view('backend');
    }

}

Try it. Should work(unless I had some typos Wink ). BTW, base controllers dont use index() methods. And always use __construct as constructor(you had some mixed constructors with PHP4/PHP5 style in your code).