CodeIgniter Forums
[Solved] MY_Router.php Set Route Question - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: [Solved] MY_Router.php Set Route Question (/showthread.php?tid=65703)



[Solved] MY_Router.php Set Route Question - wolfgang1983 - 07-13-2016

I have a custom MY_Router.php file to work to detect url when have it like

index.php?route=account/register

It picks the account up as directory and register for the controller fine.

If there when I want to get edit function on the account controller I would like to be able just to get like so.

index.php?route=account/edit

So because there is a controller

controllers > account
controllers > account > Register.php
controllers > account > Account.php // There is a edit function in this file.

Currently to get function I have to type

index.php?route=account/account&function=edit


PHP Code:
<?php

class MY_Router extends CI_Router {
    
    protected function 
_set_routing() {
    
        if (
file_exists(APPPATH.'config/routes.php'))
        {
            include(
APPPATH.'config/routes.php');
        }

        if (
file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
        {
            include(
APPPATH.'config/'.ENVIRONMENT.'/routes.php');
        }

        
// Validate & get reserved routes
        
if (isset($route) && is_array($route))
        {
            isset(
$route['default_controller']) && $this->default_controller $route['default_controller'];
            isset(
$route['translate_uri_dashes']) && $this->translate_uri_dashes $route['translate_uri_dashes'];
            unset(
$route['default_controller'], $route['translate_uri_dashes']);
            
$this->routes $route;
        }

        if (
$this->enable_query_strings) {
            
            if ( ! isset(
$this->directory))
            {
                
$route = isset($_GET['route']) ? trim($_GET['route'], " \t\n\r\0\x0B/") : '';

                if (
$route !== '')
                {
                    
$part explode('/'$route);

                    
$this->uri->filter_uri($part[0]);
                    
$this->set_directory($part[0]);

                    if ( ! empty(
$part[1])) {

                        
$this->uri->filter_uri($part[1]);
                        
$this->set_class($part[1]);

                        
// Testing only
                        
if ( ! empty($_GET['function']))
                        {
                            
$this->uri->filter_uri($_GET['function']);
                            
$this->set_method($_GET['function']);
                        }

                        
$this->uri->rsegments = array(
                            
=> $this->class,
                            
=> $this->method
                        
);
                    }

                } else {

                    
$this->_set_default_controller();
                }
            }

            
// Routing rules don't apply to query strings and we don't need to detect
            // directories, so we're done here
            
return;
        }

        
// Is there anything to parse?
        
if ($this->uri->uri_string !== '')
        {
            
$this->_parse_routes();
        }
        else
        {
            
$this->_set_default_controller();
        }
    }




RE: MY_Router.php Set Route Question - PaulD - 07-13-2016

I had trouble with this once and if I recall it was while building a todo list. I had something like:

tasks.php
tasks/full_task.php
tasks/sub_tasks.php

The problem was that CI did not know where to look of course. It did not know if tasks/sub_tasks was a method in tasks.php or a controller in the actual folder tasks. The answer was to not name folders the same as controllers.

Hope that might be of some help,

Paul.


RE: MY_Router.php Set Route Question - wolfgang1983 - 07-13-2016

(07-13-2016, 07:20 PM)PaulD Wrote: I had trouble with this once and if I recall it was while building a todo list. I had something like:

tasks.php
tasks/full_task.php
tasks/sub_tasks.php

The problem was that CI did not know where to look of course. It did not know if tasks/sub_tasks was a method in tasks.php or a controller in the actual folder tasks. The answer was to not name folders the same as controllers.

Hope that might be of some help,

Paul.

I have got it to work

PHP Code:
<?php

class MY_Router extends CI_Router {

 
   protected function _set_routing() {

 
       if (file_exists(APPPATH.'config/routes.php'))
 
       {
 
           include(APPPATH.'config/routes.php');
 
       }

 
       if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
 
       {
 
           include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
 
       }

 
       // Validate & get reserved routes
 
       if (isset($route) && is_array($route))
 
       {
 
           isset($route['default_controller']) && $this->default_controller $route['default_controller'];
 
           isset($route['translate_uri_dashes']) && $this->translate_uri_dashes $route['translate_uri_dashes'];
 
           unset($route['default_controller'], $route['translate_uri_dashes']);
 
           $this->routes $route;
 
       }

 
       if ($this->enable_query_strings) {

 
           if ( ! isset($this->directory))
 
           {

 
               $_route $this->config->item('route_trigger');
 
               $_route = isset($_GET[$_route]) ? trim($_GET[$_route], " \t\n\r\0\x0B/") : '';

 
               if ($_route !== '')
 
               {
 
                   $part explode('/'$_route);

 
                   if ( ! empty($part[1])) {

 
                       if (file_exists(APPPATH 'controllers/' $part[0] . '/' ucfirst($part[1]) . '.php')) {

 
                           $this->uri->filter_uri($part[0]);
     
                       $this->set_directory($part[0]);

     
                       $this->uri->filter_uri($part[1]);
     
                       $this->set_class($part[1]);

     
                       $_f trim($this->config->item('function_trigger'));

     
                       if ( ! empty($_GET[$_f])) {
                                
$this->uri->filter_uri($_GET[$_f]);
                                
$this->set_method($_GET[$_f]);
                            }

                            
$this->uri->rsegments = array(
                                
=> $this->class,
                                
=> $this->method
                            
);

 
                       } else {

     
                       $this->uri->filter_uri($part[0]);
     
                       $this->set_directory($part[0]);
     
                       $this->set_class($part[0]);
     
     
                            $this
->uri->filter_uri($part[1]);
     
                       $this->set_method($part[1]);    

                            $this
->uri->rsegments = array(
     
                           1 => $this->class,
     
                           2 => $this->method
                            
);

 
                       }
 
                   }

 
               } else {

 
                   $this->_set_default_controller();
 
               }
 
           }

 
           // Routing rules don't apply to query strings and we don't need to detect
 
           // directories, so we're done here
 
           return;
 
       }

 
       // Is there anything to parse?
 
       if ($this->uri->uri_string !== '')
 
       {
 
           $this->_parse_routes();
 
       }
 
       else
        
{
 
           $this->_set_default_controller();
 
       }
 
   }