Welcome Guest, Not a member yet? Register   Sign In
Folder structure for controllers
#1

[eluser]pan69[/eluser]
Hi guys. I'm working a somewhat largish site and I really need to subdivide my controllers folder into sub-directories. If I don't I get a whole bunch of incoherent files sitting in one folder which is going to do my head in.

So e.g., In the 'Settings' section of my site I have a whole bunch options. I'd like to arrange the controllers for all these options in the 'settings' sub-folder of the controllers folder. However, out of the box this is going to clash. When I call ~/settings/users I actually want to call the Users controller in the controllers/settings folder.

Is this possible?

Thanks,
Luke
#2

[eluser]Aken[/eluser]
Of course you can. It's explained in the User Guide: http://ellislab.com/codeigniter/user-gui...llers.html Look for Organizing Your Controllers into Sub-folders
#3

[eluser]pan69[/eluser]
Ah. Somehow I looked completely past that. Thanks!

Any idea how to handle the following situation. In controllers/ I have a settings.php controllers. Next to that I have a settings/ folder containing all the settings sub-category controllers. With the settings.php in the controllers/ folder there seems to be a clash between the controller and sub-folder. Any ideas how to handle that situation (without renaming one or the other).

Thanks!
#4

[eluser]rt30000[/eluser]
I am curious about this as well.
#5

[eluser]Bramme[/eluser]
I believe I have read about this once in the past (somebody handled his applications like this) and I think it works right out of the box. You should give it a whirl. If it's not working, you might have to use some routes.
(routing settings/ to setttings/index for example...)
#6

[eluser]pan69[/eluser]
Figured it out. What you do is this:

Say you want have a section called 'Settings' and you want all the controllers for this section to live in a folder called settings/ inside you controllers/ folder. In the controllers folder create a folder called settings/. If you already have a controller called settings.php in your controller/ folder than this will clash. Move your settings.php controller (or create it) in the settings/ folder and rename it to your default controller as specified in the routes.php configuration file. Mine happens to be index.php

If you now browse to yourdomain.com/settings you will see that the default controller (index.php) inside the settings/ folder is executed. I assume that this technique will also work on deeper structures but I haven't tried that yet.

Hope this helps...
#7

[eluser]Bramme[/eluser]
It sure does! Thanks for the small research. I'd love to see a library that would extend CI's subfoldering (if you can make a verb from that :p), the ability to set default controllers on a subdirectory basis etc. would be awesome!
#8

[eluser]pan69[/eluser]
[quote author="Bramme" date="1224552689"].the ability to set default controllers on a subdirectory basis etc. would be awesome![/quote]

Yeah. It would be a nice to have. I'd rather see domain specific configurations however...
#9

[eluser]sophistry[/eluser]
[quote author="Bramme" date="1224552689"]the ability to set default controllers on a subdirectory basis etc. would be awesome![/quote]

why not use custom routes? one per subdir?

OTTOMH (as in, not tested!)
Code:
$route['subdir/(:any)']='subdir/default_controller/$1';
#10

[eluser]Pascal Kriete[/eluser]
Or just extend the router. I have used the following on pretty much everything I've done. It assumes that the default controller file has the same name as the subfolder. So calling example.com/settings will go to controllers/settings/settings.php, and execute the index method.
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* Router Class
*
* Extends CI Router
*
* @author     Original by EllisLab - extension by Pascal Kriete
* @see        http://codeigniter.com
*/

class MY_Router extends CI_Router {
    
    /**
     * Constructor
     *
     * @access    public
     */
    function MY_Router()
    {
        parent::CI_Router();
    }
    
    // --------------------------------------------------------------------
    
    /**
     * Validate Routing Request
     *
     * @access    public
     */
    function _validate_request($segments)
    {
        // Check the root folder first
        if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        {
            return $segments;
        }

        // Not in the root, but not enough segments
        if (count($segments) < 2)
        {
            //Calling the index function of a controller of the same directory...
            //We'll cheat and just set our segment
            $segments[1] = $segments[0];
        }

        // Does the requested controller exist as a full path including the directory?
        if (file_exists(APPPATH.'controllers/'.$segments[0].'/'.$segments[1].EXT))
        {
            //Set the directory
            $this->set_directory($segments[0]);
            
            //Drop the directory segment
            $segments = array_slice($segments, 1);
            return $segments;
        }
        
        //Ok, that didn't work, let's try duplicating segment 0, maybe it's the same ;).
        if (file_exists(APPPATH.'controllers/'.$segments[0].'/'.$segments[0].EXT))
        {
            //Set the directory
            $this->set_directory($segments[0]);
            
            //We cheated so there's nothing to drop
            return $segments;
        }

        // Can't find the requested controller...
        show_404();
    }
}
// END MY_Router class

/* End of file MY_Router.php */
/* Location: ./application/libraries/MY_Router.php */




Theme © iAndrew 2016 - Forum software by © MyBB