CodeIgniter Forums
Best way to have multiple parent controllers - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Best way to have multiple parent controllers (/showthread.php?tid=18326)

Pages: 1 2


Best way to have multiple parent controllers - El Forum - 05-02-2009

[eluser]opel[/eluser]
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 ?


Best way to have multiple parent controllers - El Forum - 05-02-2009

[eluser]Yorick Peterse[/eluser]
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 ?


Best way to have multiple parent controllers - El Forum - 05-02-2009

[eluser]opel[/eluser]
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.


Best way to have multiple parent controllers - El Forum - 05-02-2009

[eluser]wiredesignz[/eluser]
[quote author="Opel" date="1241323840"]

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 ?[/quote]

Modular Extensions HMVC is still supported.

[quote author="Yorick Peterse" date="1241323980"]...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 ?[/quote]

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.


Best way to have multiple parent controllers - El Forum - 05-02-2009

[eluser]Rick Jolly[/eluser]
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.


Best way to have multiple parent controllers - El Forum - 05-03-2009

[eluser]Dam1an[/eluser]
[quote author="Yorick Peterse" date="1241323980"]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 ?[/quote]

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


Best way to have multiple parent controllers - El Forum - 05-03-2009

[eluser]opel[/eluser]
Quote: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 ?


Best way to have multiple parent controllers - El Forum - 05-03-2009

[eluser]wiredesignz[/eluser]
Code:
<?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 */

Code:
<?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 */



Best way to have multiple parent controllers - El Forum - 05-03-2009

[eluser]Yorick Peterse[/eluser]
[quote author="Dam1an" date="1241355137"][quote author="Yorick Peterse" date="1241323980"]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 ?[/quote]

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[/quote]

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.


Best way to have multiple parent controllers - El Forum - 05-03-2009

[eluser]opel[/eluser]
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 !