Route to multiple sub folders in CodeIgniter
Asked Answered
G

4

8

I have a admin folder set up in my controllers directory, under that i have 3 seperate sub-folders with controllers inside of them.

-- Controllers
---- Admin
------ Dashboard
-------- dashboard.php
-------- file.php
------ Members
-------- members.php
-------- file.php
------ Settings
-------- settings.php
-------- file.php

I tried routing it in the routes.php file like this

$route['admin/(:any)/(:any)'] = 'admin/$1/$2';
$route['admin/(:any)'] = 'admin/$1/$1';
$route['admin'] = 'admin/index';

What do I do to fix this?

Gelatin answered 6/3, 2011 at 20:20 Comment(0)
G
5

"Out of the Box" codeigniter does not support multiple subdirectory levels in your controllers directory, just one.

There is a way to extend the routing class to support this, check this blog entry.

Gonion answered 6/3, 2011 at 22:37 Comment(1)
Is this still the case in version 3?Caliginous
S
11

This code was already on the internet but i modified it to make it work for codeigniter 2.1

See the old source here: http://glennpratama.wordpress.com/2009/10/20/multi-level-subfolder-for-controller-in-codeigniter/

Make a new file MY_Router.php in the application/core directory, copy the following code in it:

<?php

/*
 * Custom router function v 0.2
 *
 * Add functionality : read into more than one sub-folder
 *
 */

Class MY_Router extends CI_Router
{
    Function MY_Router()
    {
        parent::__construct();
    }

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

            /* ----------- ADDED CODE ------------ */

            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]);
            if (substr($this->directory, -1, 1) == '/')
                $this->directory = $this->directory . $segments[0];
            else
                $this->directory = $this->directory . '/' . $segments[0];

            $segments = array_slice($segments, 1);
            }

            if (substr($this->directory, -1, 1) != '/')
                $this->directory = $this->directory . '/';

            /* ----------- END ------------ */

            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]);
    }
}
Squeteague answered 6/12, 2012 at 21:1 Comment(1)
This is a updated variant of the code linked to in the accepted answer by jondavidjohn.Groggery
G
5

"Out of the Box" codeigniter does not support multiple subdirectory levels in your controllers directory, just one.

There is a way to extend the routing class to support this, check this blog entry.

Gonion answered 6/3, 2011 at 22:37 Comment(1)
Is this still the case in version 3?Caliginous
P
3

For Codeigniter 3.x compatibility: The usage of the EXT constant has been deprecated since dropping support for PHP 4. There’s no longer a need to maintain different filename extensions and in this new CodeIgniter version (3.x), the EXT constant has been removed. Use just ‘.php’ instead.

So the new MY_Router.php:

<?php

/*
 * Custom router function v 0.3
 *
 * Add functionality : read into more than one sub-folder
 * Compatible with Codeigniter 3.x
 *
 */

Class MY_Router extends CI_Router
{
    Function MY_Router()
    {
        parent::__construct();
    }

    function _validate_request($segments)
    {

       if (file_exists(APPPATH.'controllers/'.$segments[0].".php"))
        {
            return $segments;
        }

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

            /* ----------- ADDED CODE ------------ */

            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]);
            if (substr($this->directory, -1, 1) == '/')
                $this->directory = $this->directory . $segments[0];
            else
                $this->directory = $this->directory . '/' . $segments[0];

            $segments = array_slice($segments, 1);
            }

            if (substr($this->directory, -1, 1) != '/')
                $this->directory = $this->directory . '/';

            /* ----------- END ------------ */

            if (count($segments) > 0)
            {

                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().'/'.$segments[0].".php"))
                {
                    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.".php"))
                {
                    $this->directory = '';
                    return array();
                }

            }

            return $segments;
        }

        show_404($segments[0]);
    }
}
Partible answered 30/10, 2016 at 15:53 Comment(0)
E
1

I was facing problem with 4-5 levels of sub-directories(like /controllers/folder1/folder2/folder3/folder4/my-controller) and change the while loop from

while(count($segments) > 0 && 
     // checks only $this->directory having a /
     is_dir(APPPATH.'controllers/'.$this->directory.$segments[0]))

to

while(count($segments) > 0 && 
   // check $this->directory having a /
  (is_dir(APPPATH.'controllers/'.$this->directory.$segments[0]) ||  
      // check $this->directory not having /
      is_dir(APPPATH.'controllers/'.$this->directory.'/'.$segments[0])))

It works for me.

The above one is ok for 2-3 sub-directories but not for 4-5 sub-directory hierarchy.

Eagleeyed answered 27/2, 2014 at 4:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.