EllisLab text mark
Advanced Search
     
Best way to have multiple parent controllers
Posted: 02 May 2009 08:10 PM
Avatar
Joined: 2007-08-07
226 posts

I have managed to get the agency I work for to try out Codeigniter and using it on a number of projects but our 4 developers (including myself) can’t agree on the best (cleanest and most efficient way) of having multiple parent controllers.

What we are trying to do is have a parent controller for frontend, admin and in some occasions member/client areas so in all 3 controllers with sub controllers that extend each. The purpose is to pass global information such as navigation, footer info and sessions etc withough having to included it in each controller.

The 3 different things we have tried :

1. I propsed using HMVC and the hooks which was successfull although required switch statement and rest of team werent keep on using a 3rd party code that was no longer supported.

2. We extended controller with My_Controller functionality for admin area but then put front_controller in the “controllers” directory and extended from it using a require statement ata top of each sub controller, decided this could be neater. One of the team added to the system/codeigniter.php file which was messy and would be a problem if we updated.

3. Was to create a library for each which seems ok idea but not sure if it a best practice.

Please could anyone give advice or tips ?

 Signature 

Portfolio: Portfolio
Twitter: @gunkdesign

 
Posted: 02 May 2009 08:13 PM   [ # 1 ]   [ Rating: 0 ]
Joined: 2009-05-02
553 posts

You could make 3 controllers, controller 1, controller 2 and controller 3. Next thing you need to do is to make controller 3 extend controller 2 and controller 2 should be extending controller 1. However, doesn’t this kill the general idea behind Object Oriented programming ?

 
Posted: 02 May 2009 08:16 PM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2007-08-07
226 posts

THanks for your suggestion.

The thing is the admin controller has data about session levels etc so things like page, blog CRUD pages would be extended e.g

Admin controllers

Admin extends Controller ()


Page extends Admin ()


And so on, I understand the principals and CI has the way to extend the controllers once through using MY_Controller but what I need is best way to have multiple instances.

 Signature 

Portfolio: Portfolio
Twitter: @gunkdesign

 
Posted: 02 May 2009 09:51 PM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-10
2937 posts
Opel - 03 May 2009 12:10 AM

1. I propsed using HMVC and the hooks which was successfull although required switch statement and rest of team werent keep on using a 3rd party code that was no longer supported.

Please could anyone give advice or tips ?

Modular Extensions HMVC is still supported.

Yorick Peterse - 03 May 2009 12:13 AM

...Next thing you need to do is to make controller 3 extend controller 2 and controller 2 should be extending controller 1. However, doesn’t this kill the general idea behind Object Oriented programming ?

Alas poor Yorick, That doesn’t kill anything, it IS the whole idea behind object oriented programming.

@Opel,

MY_Controller does not have to be a parent class.

The MY_Controller.php file may be used as a container to hold as many controller base classes as you require. Alternatively add your discrete controller base class files to application/libraries and “include” them from within MY_Controller.php

Note that this is MX_Controller.php when using HMVC.

Good luck.

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

 
Posted: 03 May 2009 12:57 AM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-06
743 posts

I don’t mind the include/require at the top of each child controller class - it’s self documenting. However, you could use wiredesignz’s suggestion, or if your parent controllers follow a naming convention, you could register an __autoload() function and require/include them through there. The __autoload() could be registered in the index.php bootstrap.

 Signature 

“I am the terror that flaps in the night”

 
Posted: 03 May 2009 04:52 AM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2008-05-18
2398 posts
Yorick Peterse - 03 May 2009 12:13 AM

You could make 3 controllers, controller 1, controller 2 and controller 3. Next thing you need to do is to make controller 3 extend controller 2 and controller 2 should be extending controller 1. However, doesn’t this kill the general idea behind Object Oriented programming ?

How would that kill OOP? One of the big things with OOP is inheritence, so having 10+ levels of inheritence is fine, as long as it makes sense to do so

 Signature 

I’m building a Project Management System for my 3rd year Uni project, Sign up to the beta
Track my progress | Post of the day: UI Designs
Get full auto complete support for CodeIgniter in Eclipse

 
Posted: 03 May 2009 06:26 AM   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2007-08-07
226 posts

The MY_Controller.php file may be used as a container to hold as many controller base classes as you require. Alternatively add your discrete controller base class files to application/libraries and “include” them from within MY_Controller.php

Would you please be able to provide an example of what you mean by this please ?

 Signature 

Portfolio: Portfolio
Twitter: @gunkdesign

 
Posted: 03 May 2009 06:39 AM   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-10
2937 posts
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Blog Controller
 */
class Blog_Controller extends Controller
{
    
function Blog_Controller() {
        parent
::Controller();
    
}
}

/**
 * Admin Controller
 */
class Admin_Controller extends Controller
{    
    
function Admin_Controller() {
        parent
::Controller();
    
}
}

/* End of file MY_Controller.php */
/* Location: ./application/libraries/MY_Controller.php */ 
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * include Blog Controller class
 */
include_once 'Blog_controller'.EXT;

/**
 * include Admin Controller class
 */
include_once 'Admin_controller'.EXT;

/* End of file MY_Controller.php */
/* Location: ./application/libraries/MY_Controller.php */ 
 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

 
Posted: 03 May 2009 06:49 AM   [ # 8 ]   [ Rating: 0 ]
Joined: 2009-05-02
553 posts
Dam1an - 03 May 2009 08:52 AM
Yorick Peterse - 03 May 2009 12:13 AM

You could make 3 controllers, controller 1, controller 2 and controller 3. Next thing you need to do is to make controller 3 extend controller 2 and controller 2 should be extending controller 1. However, doesn’t this kill the general idea behind Object Oriented programming ?

How would that kill OOP? One of the big things with OOP is inheritence, so having 10+ levels of inheritence is fine, as long as it makes sense to do so

Wasn’t OOP ment to be an idea of smaller applications (in this case classes) that would together make up one big app ? Making each controller extending the other would make you end up with one big “file” instead of multiple smaller ones.

 
Posted: 03 May 2009 07:37 AM   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2007-08-07
226 posts

thanks wiredesignz I’ll give that a whirl.

======

I used your include method and that work brilliantly. I spent all morning searching the forum for using Multiple MY_Controllers. Something CI should think about including or documenting as the 1st thing people do is create a front end backend !

 Signature 

Portfolio: Portfolio
Twitter: @gunkdesign

 
Posted: 03 May 2009 08:24 AM   [ # 10 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-10
2937 posts

You should also seriously consider Rick Jolly’s idea using __autoload() if you are developing under PHP5.

This would prevent including unnecessary base classes.

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

 
Posted: 03 May 2009 12:55 PM   [ # 11 ]   [ Rating: 0 ]
Avatar
Joined: 2007-08-07
226 posts

Ok thanks everyone for their help, for anyone that stumbles across this here is my all my code to get multiple controllers.

At the end of config.php

/*
|--------------------------------------------------------------------------
| AUTOLOAD CONTROLLERS
|--------------------------------------------------------------------------
|
*/

function __autoload($class)
{
    
if(strstr($class,'_Controller')) 
    
{
        
include_once(APPPATH 'libraries/' $class EXT);
    
}

In libraries/MY_Controller.php (Variables can be passed to all extended controllers)

class MY_Controller extends Controller
{
    
    
function MY_Controller()
    
{
        parent
::Controller();
        
}
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class 
Base_controller extends Controller 
{

    
public $navigation NULL;
    public 
$footer NULL;

    function 
Base_controller()
    
{
        parent
::Controller();
        
$this->navigation();
        
$this->footercopy();
    
}


    
        
function navigation()
        
{
            $this
->navigation['navigation'= array("Home""Test""contact");
            
$this->data['frontend_navigation'$this->load->view ('common/frontend_navigation'$this->navigationTRUE);
        
}
    
    
        
function footercopy()
        
{
            $this
->data['footer''&copy; '.date('Y').' '.$this->config->item('project');
        
}
    
 Signature 

Portfolio: Portfolio
Twitter: @gunkdesign