2016-07-14 5 views
0

Wenn ich diese URLBenutzerdefinierte MY_Router Änderung in codeigniter 3.0.6

index.php?route=account/edit 

controller > account > Account.php < - Edit-Funktion auf Controller

Der obige Code mit meiner benutzerdefinierten arbeitet MY_Router.php

Wie immer wenn ich versuche und auf einen anderen Controller zugreifen

index.php?route=account/register es funktioniert nicht

controllers > account > Register.php 

Auf meiner MY_Router.php habe ich eine Variable, die $ part [1] ist und das zweite Segment bekommt.

Wie immer muss ich der Lage sein, sicherzustellen, dass es

Wenn es einen Controller oder Funktion überprüfen

Frage: Wie kann ich sicher, wenn ich set_class verwenden und set_method dass es erkennen kann, wenn es ist ein Controller oder eine Funktion?

<?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); 

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

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

         // Testing only 
         if (! empty($part[1])) 
         { 
          $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(); 
     } 
    } 
} 

Antwort

1

Ich bin nicht sicher. Hier ist meine Idee

Add on check vor $ this-> set_class ($ part [0]);

if(file_exists(APPPATH."controllers/$part[0]/$part[1].php")){ 
    //Second parameter has controller class load 
} 
else{ 
    // controller doesn't exist 
} 
0

Dank @vijaykumar konnte es zur Arbeit zu bekommen.

<?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(
           1 => $this->class, 
           2 => $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(); 
     } 
    } 
} 
Verwandte Themen