Virajsinh

MY_Router

Nov 26th, 2019
46
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Reference URL : https://stackoverflow.com/questions/34808054/how-to-use-a-sub-folder-in-default-controller-route-in-codeigniter-3/34808170
  2.  
  3. //NOTE : user for sub folder controller not find.
  4.  
  5. //PATH : application > config > routes.php
  6. $route['default_controller'] = 'subfolder/controller/function';
  7.  
  8. //PATH : application > core > MY_Router.php
  9. <?php
  10.  
  11. class MY_Router extends CI_Router {
  12.     protected function _set_default_controller() {
  13.  
  14.         if (empty($this->default_controller)) {
  15.  
  16.             show_error('Unable to determine what should be displayed. A default route has not been specified in the routing file.');
  17.         }
  18.         // Is the method being specified?
  19.         if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2) {
  20.             $method = 'index';
  21.         }
  22.  
  23.         // This is what I added, checks if the class is a directory
  24.         if( is_dir(APPPATH.'controllers/'.$class) ) {
  25.  
  26.             // Set the class as the directory
  27.  
  28.             $this->set_directory($class);
  29.  
  30.             // $method is the class
  31.  
  32.             $class = $method;
  33.  
  34.             // Re check for slash if method has been set
  35.  
  36.             if (sscanf($method, '%[^/]/%s', $class, $method) !== 2) {
  37.                 $method = 'index';
  38.             }
  39.         }
  40.  
  41.         if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php')) {
  42.  
  43.             // This will trigger 404 later
  44.  
  45.             return;
  46.         }
  47.         $this->set_class($class);
  48.         $this->set_method($method);
  49.         // Assign routed segments, index starting from 1
  50.         $this->uri->rsegments = array(
  51.             1 => $class,
  52.             2 => $method
  53.         );
  54.         log_message('debug', 'No URI present. Default controller set.');
  55.     }
  56. }?>
RAW Paste Data