Part of the EllisLab Network
   
 
Multi Level controller not working
Posted: 30 December 2008 02:41 PM   [ Ignore ]  
Lab Assistant
RankRank
Total Posts:  111
Joined  12-13-2008

My structure for my controller is

application/controllers/admincp/config/countries.php

It’s not working when I try to access it like http://localhost/index.php/admincp/config/countries I am .getting 404 message.

If I copy that same file and put it in admincp folder (application/controllers/admincp/countries.php), it works. So my syntax is correct but it’s not letting me put controller in snd level.

Am I doing something wrong?

Thanks

Profile
 
 
Posted: 30 December 2008 03:18 PM   [ Ignore ]   [ # 1 ]  
Lab Assistant
RankRank
Total Posts:  111
Joined  12-13-2008

Just checked in system/libraries/Router.php and it only allows 1 folder in controllers.

Is there a way to over come that? My_router.php????

Profile
 
 
Posted: 30 December 2008 03:26 PM   [ Ignore ]   [ # 2 ]  
Lab Assistant
RankRank
Total Posts:  111
Joined  12-13-2008

Not sure if it’s correct but following did it

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* Router Class
*
* Extends CI Router

* @see        http://codeigniter.com
*/

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 folder?
        
if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        
{
            
return $segments;
        
}
        
        
// Is the controller in a sub-sub-folder?
        
if (is_dir(APPPATH.'controllers/'.$segments[0].'/'.$segments[1]))
        
{        
            
// Set the directory and remove it from the segment array
            
$this->set_directory($segments[0].'/'.$segments[1]);
            
$segments array_slice($segments2);
            
            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;
        
}
        
        
        
// 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($segments1);
            
            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]);
    
}
Profile
 
 
Posted: 30 December 2008 04:30 PM   [ Ignore ]   [ # 3 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2280
Joined  07-30-2007

Don’t use subfolders to manipulate the URI - that is what routing is for.

 Signature 

Follow me on twitter here.
MichaelWales.com | MichaelWales.info

Profile
 
 
Posted: 30 December 2008 04:51 PM   [ Ignore ]   [ # 4 ]  
Lab Assistant
RankRank
Total Posts:  111
Joined  12-13-2008

So, just change URLs and create routing rules?

I also wanted to do it so I could organize different controller logically. I din’t want all of them in one folder and wanted to organize them; besides routing (but din’t know I could write routing rules either smile ).

Profile
 
 
Posted: 30 December 2008 05:41 PM   [ Ignore ]   [ # 5 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2280
Joined  07-30-2007

You can still organize them better (into a single subfolder level).

With CodeIgniter there is a difference between filesystem organization and URI organization. You can literally make your URIs anything you want while still maintaining a good backend file organization.

$route['omasdofmasdfniasdkfnasiodasdf''home'
 Signature 

Follow me on twitter here.
MichaelWales.com | MichaelWales.info

Profile
 
 
Posted: 30 December 2008 05:45 PM   [ Ignore ]   [ # 6 ]  
Lab Assistant
RankRank
Total Posts:  111
Joined  12-13-2008

Sorry, I din’t understand.

So, I can still keep my sub-folders like conrollers/admincp/config/countries and then write a rule in route config file? What will it look like?

I did read the documentation but I am not sure (yet) how to do it.

will it be ??

$route['config/countries''admincp/config/countries'

Thanks

Profile
 
 
Posted: 30 December 2008 05:48 PM   [ Ignore ]   [ # 7 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2280
Joined  07-30-2007

Try it - I promise, you won’t break anything.

 Signature 

Follow me on twitter here.
MichaelWales.com | MichaelWales.info

Profile
 
 
Posted: 30 December 2008 05:52 PM   [ Ignore ]   [ # 8 ]  
Lab Assistant
RankRank
Total Posts:  111
Joined  12-13-2008

I just tried it with 2 different structures

1)

controllers/admincp/config/countries

//Tested with http://localhost/index.php/admincp/config/countries
///Not sure if it's correct but it din't work
$route['admincp/config/countries'"admincp/config/countries"

2)


controllers/admincp/countries

//Tested with http://localhost/index.php/admincp/config/countries
//Worked
$route['admincp/config/countries'"admincp/countries"

But, I was hoping to keep structure like 1) and still be able to make it work. It’s not working as I have sub-sub-level.

Sorry for being dumb but I can’t make it work

EDIT: I am assuming routes.php is for URI management and not for folder, hence won’t work for me??

Profile
 
 
Posted: 30 December 2008 05:56 PM   [ Ignore ]   [ # 9 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2280
Joined  07-30-2007

You can only go one-level deep on controllers, like we mentioned before.

./controllers
./controllers/admincp <-- One level
./controllers/admincp/countries <-- Two levels 

I’m sure if you did some searching around the forums you could find a solution for this though, I have seen it occur numerous times.

 Signature 

Follow me on twitter here.
MichaelWales.com | MichaelWales.info

Profile
 
 
Posted: 30 December 2008 05:57 PM   [ Ignore ]   [ # 10 ]  
Lab Assistant
RankRank
Total Posts:  111
Joined  12-13-2008

Thanks. I did find a solution (as mentioned above) by overloading Router.php library. But, I was hoping that I can find another solution.

I will search the forum but not sure what exactly to search for.

Thanks anyways, You were of great help. I din’t even remember routes.php. I’ll start using it now.

Profile