[eluser]Matt Johnson[/eluser]
[quote author="hvalente13" date="1216707699"]
Quote:CI only allows controllers one subfolder deep.
I've managed to get more than one subfolder level like this:
Code:
application
|
+---- controllers
|
+---- folder1
|
+---- subfolder1
And then you can link to the controllers in subfolder1 by
Code:
base_url().'index.php/folder1/subfolder1/controller_app/function'
[/quote]
I'm not sure how that's working for you as that's what I've basically tried to do.
In any event, I ended up creating my own Router class (copy and pasted the default Router class).
I then modified the function/method _validate_request() to be able to handle multiple sub-folders. By default, obviously, the system only looks to see if the first segment (segment[0]) is the directory we want. However, I just wrote some code in a loop to see what the deepest directory structure we could make.
In other words, if the segment is folder1/subfolder1/controller then it'll check to see if folder1 is a directory, then folder1/subfolder1 and then finally fail on folder1/subfolder1/controller since it isn't a directory, thereby making folder1/subfolder1 the longest folder. Then it uses this as the folder and clears the segment array of the directory related entries (in this case 'folder1' and 'subfolder1').
Here's the code I wrote. I suspect there are better/more efficient solutions but this is what I came up with in 10 minutes in case anyone is looking for a solution. (I add the code between NEW CODE and END NEW CODE and also comment out the two lines before it)
Code:
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]))
{
// Set the directory and remove it from the segment array
//$this->set_directory($segments[0]);
//$segments = array_slice($segments, 1);
/* -- NEW CODE -- */
$tempDir = array();
$i = 0;
for(; $i < count($segments); $i++)
{
// We keep going until we can't find a directory
$tempDir[] = $segments[$i];
if(!is_dir(APPPATH.'/controllers/'.implode('/', $tempDir)))
{
// The last "segment" is not a part of the "directory" so we can get rid of it.
unset($tempDir[count($tempDir)-1]);
break;
}
}
$this->set_directory(implode('/', $tempDir));
$segments = array_slice($segments, $i);
/* -- END NEW CODE -- */
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]);
}
Please let me know if you find any errors/problems. I suppose this should work with any number of sub-directories.