CodeIgniter Forums
Multi Level controller not working - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Multi Level controller not working (/thread-14358.html)

Pages: 1 2


Multi Level controller not working - El Forum - 12-30-2008

[eluser]jaswinder_rana[/eluser]
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


Multi Level controller not working - El Forum - 12-30-2008

[eluser]jaswinder_rana[/eluser]
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????


Multi Level controller not working - El Forum - 12-30-2008

[eluser]jaswinder_rana[/eluser]
Not sure if it's correct but following did it

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



Multi Level controller not working - El Forum - 12-30-2008

[eluser]Michael Wales[/eluser]
Don't use subfolders to manipulate the URI - that is what routing is for.


Multi Level controller not working - El Forum - 12-30-2008

[eluser]jaswinder_rana[/eluser]
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 ).


Multi Level controller not working - El Forum - 12-30-2008

[eluser]Michael Wales[/eluser]
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.

Code:
$route['omasdofmasdfniasdkfnasiodasdf'] = 'home';



Multi Level controller not working - El Forum - 12-30-2008

[eluser]jaswinder_rana[/eluser]
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 ??
Code:
$route['config/countries'] = 'admincp/config/countries';

Thanks


Multi Level controller not working - El Forum - 12-30-2008

[eluser]Michael Wales[/eluser]
Try it - I promise, you won't break anything.


Multi Level controller not working - El Forum - 12-30-2008

[eluser]jaswinder_rana[/eluser]
I just tried it with 2 different structures

1)

controllers/admincp/config/countries
Code:
//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
Code:
//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??


Multi Level controller not working - El Forum - 12-30-2008

[eluser]Michael Wales[/eluser]
You can only go one-level deep on controllers, like we mentioned before.

Code:
./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.