CodeIgniter Forums

Full Version: Controllers in two level subdirectory
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]adaxa[/eluser]
Is there any hack to provide two level sub directory support ?

El Forum

[eluser]Fireclave[/eluser]
Look here

http://ellislab.com/codeigniter/user-gui...subfolders

El Forum

[eluser]WanWizard[/eluser]
Not without extending the loader class. By default, only one level deep is checked, and you can't have a controller and a folder with the same name (the controller is always loaded first).

El Forum

[eluser]adaxa[/eluser]
Here is the solution
Code:
<?php

Class MY_Router extends CI_Router
{
    function MY_Router()
    {
        parent::CI_Router();
    }

    function _validate_request($segments)
    {
        if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        {
            return $segments;
        }

        if (is_dir(APPPATH.'controllers/'.$segments[0]))
        {
            $this->set_directory($segments[0]);
            $segments = array_slice($segments, 1);

            while(count($segments) > 0 && is_dir(APPPATH.'controllers/'.$this->directory.$segments[0]))
            {
                // Set the directory and remove it from the segment array
            $this->set_directory($this->directory . $segments[0]);
            $segments = array_slice($segments, 1);
            }

            if (count($segments) > 0)
            {
                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');

                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                {
                    $this->directory = '';
                    return array();
                }

            }

            return $segments;
        }

        show_404($segments[0]);
    }
}

?>

El Forum

[eluser]ZoranM[/eluser]
Dear Adaxa,
Thanks for the fast solution of this problem that bothers me too due to same reasons - my application became quite complex and I need more than one subfolder level too.
However, I am not sure I know how to implement your solution so please advice.

Thanks in advance,
Zoran

El Forum

[eluser]InsiteFX[/eluser]
Then maybe use HMVC.

InsiteFX

El Forum

[eluser]ZoranM[/eluser]
Hi,

Thanks for a quick reply. This HVMC story opens up a new world for me, I will propably turn into it if this doesn't work for me eventually.

In the meantime, I found also this thread:
http://ellislab.com/forums/viewthread/94258/
so I understood that I should put this code into file called MY_Router.php and put file into libraries folder.
I also loaded that file in my admin page controller:
Code:
$this->load->library('MY_Router');
Then this controller calls view with a menu whose options are controllers in subdirs. When
I click on menu option to call controller in 2 level subdir I still get 404 error.
What am I doing wrong?

Please advice and sorry for my poor English.

Thanks,
Zoran

El Forum

[eluser]ZoranM[/eluser]
I also tried to put this MY_Router.php into application/core directory, according to instructions in http://ellislab.com/codeigniter/user-gui...asses.html
Declarations are:
Code:
Class MY_Router extends CI_Router
{
    function MY_Router()
    {
        parent::CI_Router();
    }

    function _validate_request($segments)
    {
...
After all this I am still not sure how to invoke this change of the core... Still getting 404 error.

El Forum

[eluser]ZoranM[/eluser]
Dear all,
My deepest appology for all these posts made most because of fatigue and not seeing obvious and documented. Hopefully this will spare someone else's pain and time from now on Smile.
Because of increased complexity of my app., I needed to have structure like this:
system/application/controllers/admin/user/whos_online.php
As you can see, this is not allowed structure in CI, since it has sub-subfolder.
After reading several threads and experimenting, this is what worked for me:
1. put above code in MY_Router.php file
2. put MY_Router.php file in system/application/libraries folder

That's all. I was mislead by http://ellislab.com/codeigniter/user-gui...asses.html and I am still curious why that does not work.

Thanks for help!
Happy coding,
Regards,
Zoran

El Forum

[eluser]gbar[/eluser]
Hi ZoranM, can you tell me with which version of Codeigniter you've got your success? I followed your same steps with 2.0.2, but I can't get to a second folder as I want.
I copied and pasted the above code in a file MY_Router.php, taking care to change the constructor in:

Code:
class MY_Router extends CI_Router
{
    function __construct()
    {
        parent::__construct();
    }

    function _validate_request($segments)...

I tried to put the file in applications/libraries that applications/core, but I always get a 404.

I am developing an application that is becoming quite large, I need to have other subfolders.

At the moment, seems excessive to me as HMVC acquire other knowledge, not even before you have completed the base path. Surely in a next time, but not now.

Does anyone have any suggestions?

Thanks
Pages: 1 2