Welcome Guest, Not a member yet? Register   Sign In
controller nested folders
#28

[eluser]TheFuzzy0ne[/eluser]
I
I have problems navigating around code that's any more than a few pages in length, so I now try to export every controller method into it's own controller, and use _remap(). So my structure would look something like this.

Code:
controllers
  +-admin
      +-forums
      |   +-homepage.php
      |   +-delete.php
      |   +-create.php
      |   +-update.php
      |   +-show.php
      |
      +-users
      |   +-homepage.php
      |   +-delete.php
      |   +-create.php
      |   +-update.php
      |   +-show.php
      |
      +-homepage.php
      +-another_controller.php
The standard CodeIgniter Router does not go more than one directory deep, so I needed an alternative.

Here's my take on the subject.

./system/application/libraries/MY_Router.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Router extends CI_Router {
    
    /**
     * Validates the supplied segments.  Attempts to determine the path to
     * the controller.
     *
     * @access    private
     * @param    array
     * @return    array
     */    
    function _validate_request($segments)
    {
        
        // Does the requested controller exist in the root directory?
        if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        {
            return $segments;
        }

        // Maybe it's in a sub-directory?
        while (count($segments) > 0)
        {    
            if (file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
            {
                return $segments;
            }
            
            $this->set_directory($this->fetch_directory().$segments[0]);
            $segments = array_slice($segments, 1);
        }
        
        // We're out of options - is there a default controller in the current directory?
        if (file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
        {
            $this->set_class($this->default_controller);
            $this->set_method('index');
            
            return array();
        }
        
        # I was thinking of also imeplementing something like this:
        
        #// Is there a file named "index" in the current directory?
        #if (file_exists(APPPATH.'controllers/'.$this->fetch_directory()."index".EXT))
        #{
        #    $this->set_class($this->default_controller);
        #    $this->set_method('index');
        #    
        #    return array();
        #}
        
        # ... and/or this:
        
        #// Is there a file with the same name as the current directory
        #if (file_exists(APPPATH.'controllers/'.$this->fetch_directory().basename($this->fetch_directory()).EXT))
        #{
        #    $this->set_class($this->default_controller);
        #    $this->set_method('index');
        #    
        #    return array();
        #}
        
        // Can't find the requested controller...
        show_404(substr($this->fetch_directory(), 0, -1));
    }
}

The last two if statements was something I was thinking of implementing, so that apps can be more portable, and not have to know what the name of the default controller is.

However, if you create a subdirectory with the same name as a PHP file that resides in the same directory, the directory will never be traversed. Blatantly obvious, but hey...

And also, it's recommended that each controller directory contains a default controller. Again, this is more to do with common sense than anything else.

Feel free to give this library a bash. It's been designed to be fully compatible with your current structure, so long as your structure works with CodeIgniter already.

Comments and questions welcome.

@Dam1an: I've edited my post above to reflect the current situation accordingly. Smile


Messages In This Thread
controller nested folders - by El Forum - 04-29-2009, 01:42 PM
controller nested folders - by El Forum - 04-29-2009, 01:44 PM
controller nested folders - by El Forum - 04-29-2009, 01:48 PM
controller nested folders - by El Forum - 04-29-2009, 01:57 PM
controller nested folders - by El Forum - 04-29-2009, 02:34 PM
controller nested folders - by El Forum - 04-29-2009, 03:18 PM
controller nested folders - by El Forum - 04-29-2009, 10:39 PM
controller nested folders - by El Forum - 04-30-2009, 06:28 AM
controller nested folders - by El Forum - 04-30-2009, 06:30 AM
controller nested folders - by El Forum - 04-30-2009, 07:04 AM
controller nested folders - by El Forum - 04-30-2009, 07:08 AM
controller nested folders - by El Forum - 04-30-2009, 07:09 AM
controller nested folders - by El Forum - 04-30-2009, 07:23 AM
controller nested folders - by El Forum - 04-30-2009, 07:52 AM
controller nested folders - by El Forum - 04-30-2009, 07:57 AM
controller nested folders - by El Forum - 04-30-2009, 08:01 AM
controller nested folders - by El Forum - 04-30-2009, 08:14 AM
controller nested folders - by El Forum - 05-04-2009, 07:37 AM
controller nested folders - by El Forum - 05-04-2009, 09:06 AM
controller nested folders - by El Forum - 05-04-2009, 10:04 AM
controller nested folders - by El Forum - 05-17-2009, 03:31 PM
controller nested folders - by El Forum - 05-17-2009, 04:03 PM
controller nested folders - by El Forum - 05-17-2009, 04:46 PM
controller nested folders - by El Forum - 05-17-2009, 05:34 PM
controller nested folders - by El Forum - 05-18-2009, 03:04 AM
controller nested folders - by El Forum - 05-18-2009, 03:06 AM
controller nested folders - by El Forum - 05-18-2009, 03:11 AM
controller nested folders - by El Forum - 06-01-2009, 06:24 AM
controller nested folders - by El Forum - 06-01-2009, 06:39 AM
controller nested folders - by El Forum - 06-01-2009, 06:45 AM
controller nested folders - by El Forum - 06-01-2009, 06:47 AM
controller nested folders - by El Forum - 06-01-2009, 06:48 AM
controller nested folders - by El Forum - 06-01-2009, 12:03 PM
controller nested folders - by El Forum - 07-09-2010, 01:28 PM
controller nested folders - by El Forum - 07-09-2010, 01:45 PM



Theme © iAndrew 2016 - Forum software by © MyBB