EllisLab text mark
Advanced Search
     
multi-level subfolders for controllers for 1.7.1
Posted: 17 September 2009 11:17 PM
Joined: 2009-09-17
81 posts

Just want to throw it out to the community yet another multi-level subfolders controller extension.  This is for 1.7.1.

HTH, Damien K.

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * MY_Router Class
 *
 * @author Damien K.
 */
class MY_Router extends CI_Router
{
     
function __construct()
     
{
         parent
::CI_Router();
     
}

    
// --------------------------------------------------------------------

    /**
     * OVERRIDE
     */
    
function _validate_request($segments)
    
{
        
// 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]))
        
{
            
// EDIT:
            
$dir '';
            do
            
{
                
if (strlen($dir) > 0)
                
{
                    $dir 
.= '/';
                
}
                $dir 
.= $segments[0];
                
$segments array_slice($segments1);
            
while (is_dir(APPPATH.'controllers/'.$dir .'/'.$segments[0]));
            
// Set the directory and remove it from the segment array
            
$this->set_directory($dir);
            
// END EDIT:

            
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]);
                
}
            }
            
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;
        
}

        
// Can't find the requested controller...
        
show_404($segments[0]);
    
}
}

// END MY_Router class

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

Damien K.

 
Posted: 08 February 2010 06:55 PM   [ # 1 ]   [ Rating: 0 ]
Joined: 2007-02-09
6 posts

Very nice and elegant smile

Since one could pass URI without controller name (assuming that default controller should be loaded), you should check if $segments array is not empty:

while ((is_dir(APPPATH.'controllers/'.$dir .'/'.$segments[0])) && (!empty($segments))); 

Otherwise you will end up with $dir with multiplied ‘/’ at the end of the string, probably as many as file system accepts.

 
Posted: 01 February 2011 12:54 PM   [ # 2 ]   [ Rating: 0 ]
Joined: 2010-06-16
12 posts

Hello
It looks like this hack is not compatible with CI 1.7.3 as it used to be in 1.7.2
Any idea on how to get multi level controllers in 1.7.3 ?

Thanks in advance

geoffroy

 
Posted: 11 February 2011 08:25 AM   [ # 3 ]   [ Rating: 0 ]
Joined: 2011-02-11
1 posts

I’ve moved to code igniter 2.0.0 and this alternate router stopped working.

After some debug i’ve found that in this new version they’ve updated set_directory function in the core Router.

The new version replaces all the / in the directory and replaces with “”

function set_directory($dir)
{
$this->directory = str_replace(array('/', '.'), '', $dir).'/';
}


This way the directory we’re trying to use becomes a complete different one.
In the case I was working on “backoffice/system” became “backofficesystem”...

I’ve just updated my_router and added a new/old version of set_directory

// Replaces new set_directory function in code igniter 2.0.0 with version 1.7.2
function set_directory($dir)
{ $this->directory = $dir.'/'; }


Now, its working fine again smile

 
Posted: 18 February 2011 10:24 AM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2010-12-15
85 posts

Awesome snippet, this is exactly what I need. But I’m getting this error when trying to use it.

A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Filename: core/MY_Router.php
Line Number: 43

Line 43:

while (is_dir(APPPATH 'controllers/' $dir '/' $segments[0])); 

Anyone have any ideas? Trying to use this with CodeIgniter 2.0

Thanks

 
Posted: 18 March 2011 04:35 AM   [ # 5 ]   [ Rating: 0 ]
Joined: 2011-03-02
7 posts
xerobytez - 18 February 2011 03:24 PM

Awesome snippet, this is exactly what I need. But I’m getting this error when trying to use it.

A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Filename: core/MY_Router.php
Line Number: 43

Line 43:

while (is_dir(APPPATH 'controllers/' $dir '/' $segments[0])); 

Anyone have any ideas? Trying to use this with CodeIgniter 2.0

Thanks

Just add

if(!empty($segments)) {code} 

to the your custom router.

 
Posted: 07 June 2011 05:56 PM   [ # 6 ]   [ Rating: 0 ]
Joined: 2010-12-19
2 posts

If you change the do/while loop to:

do
            
{
            
...   
            
while ($segments && is_dir(APPPATH.'controllers/'.$dir .'/'.$segments[0])); 

and then move the file_exists if statement below the is_dir if statement, you will be able to have a file structure where a controller and directory of the same name can exist in the controllers directory.

Like this:

controllers/example.php
controllers
/example/example_sub.php 
 
Posted: 12 July 2011 12:55 PM   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2011-04-14
21 posts

Hello

Very nice post. I have tested the code for 2.0.0 it works fine.

But fetching some problem when making mobile interface using jquery mobile.
And the problem occurs when you browse with an Android.

The problem is it does’t load the css, js etc.

Any idea?

 Signature 

Manas Bala
Codeigniter Developer

 
Posted: 12 July 2011 12:58 PM   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2011-04-14
21 posts
fivefinger-bd - 12 July 2011 04:55 PM

Hello

Very nice post. I have tested the code for 2.0.0 it works fine.

But fetching some problem when making mobile interface using jquery mobile.
And the problem occurs when you browse with an Android.

The problem is it does’t load the css, js etc.

Any idea?

Sorry my mistake. Problem was Android doesnot support http://localhost

It supports http://10.0.2.2

 Signature 

Manas Bala
Codeigniter Developer

 
Posted: 17 April 2014 04:40 AM   [ # 9 ]   [ Rating: 0 ]
Joined: 2014-04-08
160 posts
Damien K. - 17 September 2009 11:17 PM

Just want to throw it out to the community yet another multi-level subfolders controller extension.  This is for 1.7.1.

HTH, Damien K.

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * MY_Router Class
 *
 * @author Damien K.
 */
class MY_Router extends CI_Router
{
     
function __construct()
     
{
         parent
::CI_Router();
     
}

    
// --------------------------------------------------------------------

    /**
     * OVERRIDE
     */
    
function _validate_request($segments)
    
{
        
// 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]))
        
{
            
// EDIT:
            
$dir '';
            do
            
{
                
if (strlen($dir) > 0)
                
{
                    $dir 
.= '/';
                
}
                $dir 
.= $segments[0];
                
$segments array_slice($segments1);
            
while (is_dir(APPPATH.'controllers/'.$dir .'/'.$segments[0]));
            
// Set the directory and remove it from the segment array
            
$this->set_directory($dir);
            
// END EDIT:

            
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]);
                
}
            }
            
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;
        
}

        
// Can't find the requested controller...
        
show_404($segments[0]);
    
}
}

// END MY_Router class

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

Anyone for latest version of CodeIgniter_2.1.4 I am trying to make default router load. $route[‘default_controller’] = “frontend/common/home”;

 Signature 

Cheers From,
Riwaka Website Design
My Face Book Page