Welcome Guest, Not a member yet? Register   Sign In
Multi Level controller not working
#1

[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
#2

[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????
#3

[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]);
    }
}
#4

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

[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 ).
#6

[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';
#7

[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
#8

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

[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??
#10

[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.




Theme © iAndrew 2016 - Forum software by © MyBB