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

[eluser]Jagar[/eluser]
Actually with the help of http://ellislab.com/forums/viewthread/107451/#541043 you can have unlimited nested folders, view the code.

You can either use the posted code, or use mine, which I customized it to fit my needs, it may even help you.

Code:
<?php

/**
* Router Extension to allow controllers in unlimited nesting of folders.
* Discussion thread at: http://ellislab.com/forums/viewthread/56100/
* @author Peter Goodman
* @copyright Copyright 2007 Peter Goodman, all rights reserved.
*
* CMS capability by Adam Jackett www.darkhousemedia.com, 2009
*/
class MY_Router extends CI_Router {
    
    function _set_route_mapping() {
        parent::_set_route_mapping();
        
        // re-routed url
        if($this->rsegments != $this->segments) {
            array_unshift($this->rsegments, $this->directory);
        }
    }
    
    function _pluck_directory($segments) {
        $this->directory = '';
        
        foreach($segments as $segment) {
            $segment = trim($segment);
            if($segment != '') {
                if(is_dir(APPPATH .'controllers/'. $this->directory . $segment)) {
                    $this->directory .= array_shift($segments) .'/';
                } else {
                    break;
                }
            } else {
                array_shift($segments);
            }
        }
        
        // quick an easy forced reindexing
        $segments = array_values($segments);
        
        // put the entire directory path back into the segment as the first
        // item
        $dir = trim($this->directory, '/');
        if(!empty($dir)) {
            array_unshift($segments, $dir);
        }
        
        $this->segments = $segments;
        
        return $segments;
    }
    
    /*function _validate_request($segments) {
        return parent::_validate_request($this->_pluck_directory($segments));
    }*/
    
    function _validate_request($segments) {
        $segments = $this->_pluck_directory($segments);
        $found = TRUE;
        
        // Does the requested controller exist in the root folder?
        if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        {
            return $segments;
        }
        
        // Is the controller in a sub-folder?
        if (is_dir(APPPATH.'controllers/'.$segments[0]))
        {        
            // Set the directory and remove it from the segment array
            $this->set_directory($segments[0]);
            $segments = array_slice($segments, 1);
            
            if (count($segments) > 0)
            {
                // Does the requested controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
                {
                    //show_404($this->fetch_directory().$segments[0]);
                    $found = FALSE;
                }
            }
            else
            {
                $this->set_class($this->default_controller);
                $this->set_method('index');
            
                // Does the default controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                {
                    $this->directory = '';
                    return array();
                }
            
            }
            
            //return $segments;
            if($found){
                return $segments;
            }else {
                show_404($this->fetch_directory().$segments[0]);
            }
        }

        // Can't find the requested controller...
        //show_404($segments[0]);
        $this->set_directory('');
        $this->set_class('home'); #Whatever the default controller is
        $this->set_method('index');
        return array();
    }

}


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