Welcome Guest, Not a member yet? Register   Sign In
multi-level subfolders for controllers for 1.7.1
#1

[eluser]Damien K.[/eluser]
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.


Code:
<?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($segments, 1);
            } 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 */
#2

[eluser]krzycho[/eluser]
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:
Code:
} 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.
#3

[eluser]g_montel[/eluser]
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
#4

[eluser]Unknown[/eluser]
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
#5

[eluser]xerobytez[/eluser]
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:
Code:
} while (is_dir(APPPATH . 'controllers/' . $dir . '/' . $segments[0]));

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

Thanks
#6

[eluser]ChrisHJ[/eluser]
[quote author="xerobytez" date="1298060643"]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:
Code:
} while (is_dir(APPPATH . 'controllers/' . $dir . '/' . $segments[0]));

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

Thanks[/quote]

Just add
Code:
if(!empty($segments)) {code}
to the your custom router.
#7

[eluser]erikstraub[/eluser]
If you change the do/while loop to:
Code:
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:
Code:
controllers/example.php
controllers/example/example_sub.php
#8

[eluser]fivefinger-bd[/eluser]
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?
#9

[eluser]fivefinger-bd[/eluser]
[quote author="fivefinger-bd" date="1310504107"]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?[/quote]

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

It supports http://10.0.2.2
#10

[eluser]riwakawd[/eluser]
[quote author="Damien K." date="1253243847"]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.


Code:
<?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($segments, 1);
            } 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 */
[/quote]

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




Theme © iAndrew 2016 - Forum software by © MyBB