Welcome Guest, Not a member yet? Register   Sign In
Controller Subfolders
#1

[eluser]adamp1[/eluser]
Quick question here. How many folders deep can controllers be put in? Is it only one or can I have many levels.

Reason for me asking is I want the following

Code:
controllers/admin/system

Now the admin folder holds all controllers for the admin area (like the default landing controller), while the admin/system folder holds all controllers specific to the controllers which deal with system functionaly (settings,users etc)?

So is what I want possible?
#2

[eluser]Michael Ekoka[/eluser]
one
#3

[eluser]adamp1[/eluser]
OK cheers for clearing that up, will just have to have a super large controller then.
#4

[eluser]Michael Ekoka[/eluser]
why use a super large controller, when you can create many base controllers in your application/libraries/MY_Controller.php file.

http://ellislab.com/forums/viewthread/67463/

BTW when i say one i mean one folder deep after the controllers directory. However you need to be aware of a bug in CI regarding this:
http://ellislab.com/forums/viewthread/50795/
http://codeigniter.com/bug_tracker/bug/2849/
#5

[eluser]adamp1[/eluser]
Because that would take the controllers away from the controller folder, would mean changing things in the future would be more complex (in the fact that I would forget they were there) Also I'm trying to as much as possible not spread my code around too much since if I do release it I want similar code to be in the same place.
#6

[eluser]Michael Ekoka[/eluser]
Using base controlles provides you with great amount of flexibility, this is what MVC and OO programming is about. You don't have to create your base controllers in application/libraries/MY_Controller.php, you can move them and later include them there. Even if you want to package your code I don't see how this can be a problem.
Code:
Package
  |-controllers
     |- admin
        |- login.php
        |- management.php
        |- forms.php
     |- site
        |- registration.php
  |-libraries      
     |-Base_controllers
        |- All_Base_Controllers.php (this file would include all the others)
        |- Admin_Controller.php
        |- Application_Controller.php

If you drop the content of such a package in a CI application it will fall right in place. The only extra step you might need would be to connect the Base controllers to your application and this can be done in a simple step:

In your application/libraries/MY_Controller.php, just include the All_Base_Controllers.php file (which itself includes the others). Alternatively you can decide to simply include your base controllers at the top of your controllers files if you wish to make everything automatic for someone using your release.

in application/libraries/MY_Controller.php
Code:
include_once 'Base_Controllers/All_Base_Controllers.php';

in application/controllers/admin/management.php
Code:
// optional. Use this if you don't want to include your Base controllers from the MY_Controller.php file
include_once APPPATH.'libraries/Base_Controllers/All_Base_Controllers.php';

What would your base controllers look like: e.g. your Admin_controller.php would have something like this:
Code:
class Admin_controller extends Controller{

    function __construct(){
        // some init steps
        if(!this->_authenticate())redirect('admin/login');
        parent::__construct();
        // additional init steps (set language, db connect, authentication, cookies collect, etc)
    }
    
    private function _authenticate(){
        // if logged in session variable is set return true, else return false
    }
...
}

Then your controllers would just extend the base they need to and be ready to use:

controllers/admin/management.php
Code:
class Management extends Admin_controller{
...
}
controllers/admin/forms.php
Code:
class Forms extends Admin_controller{
...
}
controllers/admin/login.php
Code:
class Login extends Controller{
    function index(){
        // if username/password match, set the logged in session variable to true
        // else show login screen again
    }
...
}
controllers/site/registration.php
Code:
class Registration extends Application_controller{
...
}
#7

[eluser]adamp1[/eluser]
After reading your reply I don't think you quite understood why I wanted sub folders. Ill try to explain what I mean.

In my admin area at the top I have a drop down menu system much like ExpressionEngine. One option is
Code:
System
   -> Users
   -> Settings
   -> etc
If the user clicks on 'Users' I want them to go to a page showing them the list of users. If they click on 'Settings' I want it to show the settings and so on. BUT if they click on 'System' I want it to show a page which shows the sub options 'Users','Settings'. That's all this system_default controller would do.

Now for why I wanted to be able to put it in its own sub folder. This was the structure I wanted.
Code:
admin/
   system/
      default.php
      users.php
      settings.php

The reason for this mean then all code to do with managing the system is within a single folder. This would mean then you would have a new folder for each parent menu item thus keeping the code modular.

So If I wanted to add a menu item to control site pages the structure would be as follws
Code:
admin/
   pages/
     default.php
     pages.php
     templtes.php
     stylesheets.php
   system/
     default.php
     users.php
     settings.php

I could probably do what you said, but then I would be splitting up the code to view the front page of a controller and the inner subfuctions. Which to me seems to complicate the matter.

The reason I said I would have to have a large controller is it would have a single controller 'System' which would do the job of the folder (IE keep all code related to system operations together)




Theme © iAndrew 2016 - Forum software by © MyBB