Welcome Guest, Not a member yet? Register   Sign In
[Solved] Routing Question.
#1

(This post was last modified: 10-31-2015, 02:26 AM by wolfgang1983.)

I know this question problem has been asked quite a few times.

With codeigniter 3.0 + versions you are not able to have sub folders for default controller in routes.php

// Not allowed
$route['default_controller'] = subfolder/'welcome';

// Allowed
$route['default_controller'] = 'welcome';

Has any one come up with a Custom MY_Router.php that could over ride that? to allow to have sub folders in default_controller in routes on CI3 versions?

I know people say it not the norm to have it like that but would be good if could have option like what HMVC does.
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

Only the default route has this restriction, as it is the main entry point.

Why not leave the welcome controller but have a redirect to your subfolder and method you want to be the default route. That's what I am doing.
Practical guide to IgnitedCMS - Book coming soon
Reply
#3

You will need to override the _set_default_controller() method in the router class.

Create a MY_Router.php class in ./application/core and add this:

PHP Code:
class MY_Router extends CI_Router {

 
    /**
      * Set default controller
      *
      * @return void
      */
 
    protected function _set_default_controller()
 
    {
 
        if (empty($this->default_controller))
 
        {
 
            show_error('Unable to determine what should be displayed. A default route has not been specified in the routing file.');
 
        }

 
        // Is the method being specified?
 
        if (sscanf($this->default_controller'%[^/]/%s'$class$method) !== 2)
 
        {
 
            $method 'index';
 
        }
 
        
         
// This is what I added, checks if the class is a directory
 
        if( is_dir(APPPATH.'controllers/'.$class) ) {
 
            // Set the class as the directory
 
            $this->set_directory($class);
 
            // $method is the class
 
            $class $method;
 
            // Re check for slash if method has been set
 
            if (sscanf($method'%[^/]/%s'$class$method) !== 2)
 
            {
 
                $method 'index';
 
            }
 
        }

 
        if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php'))
 
        {
 
            // This will trigger 404 later
 
            return;
 
        }

 
        $this->set_class($class);
 
        $this->set_method($method);

 
        // Assign routed segments, index starting from 1
 
        $this->uri->rsegments = array(
 
            1 => $class,
 
            2 => $method
         
);

 
        log_message('debug''No URI present. Default controller set.');
 
    }

Reply
#4

(This post was last modified: 10-31-2015, 02:15 AM by wolfgang1983.)

(10-31-2015, 01:37 AM)Martin7483 Wrote: You will need to override the _set_default_controller() method in the router class.

Create a MY_Router.php class in ./application/core and add this:



PHP Code:
class MY_Router extends CI_Router {

 
    /**
      * Set default controller
      *
      * @return void
      */
 
    protected function _set_default_controller()
 
    {
 
        if (empty($this->default_controller))
 
        {
 
            show_error('Unable to determine what should be displayed. A default route has not been specified in the routing file.');
 
        }

 
        // Is the method being specified?
 
        if (sscanf($this->default_controller'%[^/]/%s'$class$method) !== 2)
 
        {
 
            $method 'index';
 
        }
 
        
         
// This is what I added, checks if the class is a directory
 
        if( is_dir(APPPATH.'controllers/'.$class) ) {
 
            // Set the class as the directory
 
            $this->set_directory($class);
 
            // $method is the class
 
            $class $method;
 
            // Re check for slash if method has been set
 
            if (sscanf($method'%[^/]/%s'$class$method) !== 2)
 
            {
 
                $method 'index';
 
            }
 
        }

 
        if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php'))
 
        {
 
            // This will trigger 404 later
 
            return;
 
        }

 
        $this->set_class($class);
 
        $this->set_method($method);

 
        // Assign routed segments, index starting from 1
 
        $this->uri->rsegments = array(
 
            1 => $class,
 
            2 => $method
         
);

 
        log_message('debug''No URI present. Default controller set.');
 
    }


That works great can it be done also for the

// Working
$route['404_override'] = 'not_found';

// Not Working
$route['404_override'] = 'error/not_found';
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#5

(10-31-2015, 12:54 AM)iamthwee Wrote: Only the default route has this restriction, as it is the main entry point.

Why not leave the welcome controller but have a redirect to your subfolder and method you want to be the default route. That's what I am doing.

I like to have most of the controllers in sub folders inside the controllers folder keeps it nice and tidy and separated but that is my opinion.

Every one has there own ideas.
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#6

I would not advise doing that as you would need to edit ./system/core/CodeIgniter.php file. And it is never a good idea to edit the core files.

But if you do want to do that this should work:
PHP Code:
if( is_dir(APPPATH.'controllers/'.$error_class) ) {
 
   // Set the class as the directory
 
   $RTR->set_directory($error_class);
 
   // $method is the class
 
   $error_class $error_method;
 
   // Re check for slash if method has been set
 
   if (sscanf($error_method'%[^/]/%s'$error_class$error_method) !== 2)
 
   {
 
       $error_method 'index';
 
   }


Place it after these lines, round line number 433-436:
PHP Code:
if (sscanf($RTR->routes['404_override'], '%[^/]/%s'$error_class$error_method) !== 2)
{
    $error_method 'index';

Reply
#7

(10-31-2015, 02:23 AM)Martin7483 Wrote: I would not advise doing that as you would need to edit ./system/core/CodeIgniter.php file. And it is never a good idea to edit the core files.

But if you do want to do that this should work:

PHP Code:
if( is_dir(APPPATH.'controllers/'.$error_class) ) {
 
   // Set the class as the directory
 
   $RTR->set_directory($error_class);
 
   // $method is the class
 
   $error_class $error_method;
 
   // Re check for slash if method has been set
 
   if (sscanf($error_method'%[^/]/%s'$error_class$error_method) !== 2)
 
   {
 
       $error_method 'index';
 
   }


Place it after these lines, round line number 433-436:

PHP Code:
if (sscanf($RTR->routes['404_override'], '%[^/]/%s'$error_class$error_method) !== 2)
{
    $error_method 'index';


Thanks for your help and advice
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB