Example #1
0
 /**
  * Router setup routine. Automatically called during Kohana setup process.
  *
  * @return  void
  */
 public static function setup()
 {
     if (!empty($_SERVER['QUERY_STRING'])) {
         // Set the query string to the current query string
         self::$query_string = '?' . trim($_SERVER['QUERY_STRING'], '&/');
     }
     if (self::$routes === NULL) {
         // Load routes
         self::$routes = Kohana::config('routes');
     }
     // Default route status
     $default_route = FALSE;
     if (self::$current_uri === '') {
         // Make sure the default route is set
         if (!isset(self::$routes['_default'])) {
             throw new Kohana_Exception('core.no_default_route');
         }
         // Use the default route when no segments exist
         self::$current_uri = self::$routes['_default'];
         // Default route is in use
         $default_route = TRUE;
     }
     // Make sure the URL is not tainted with HTML characters
     self::$current_uri = html::specialchars(self::$current_uri, FALSE);
     // Remove all dot-paths from the URI, they are not valid
     self::$current_uri = preg_replace('#\\.[\\s./]*/#', '', self::$current_uri);
     // At this point segments, rsegments, and current URI are all the same
     self::$segments = self::$rsegments = self::$current_uri = trim(self::$current_uri, '/');
     // Set the complete URI
     self::$complete_uri = self::$current_uri . self::$query_string;
     // Explode the segments by slashes
     self::$segments = ($default_route === TRUE or self::$segments === '') ? array() : explode('/', self::$segments);
     if ($default_route === FALSE and count(self::$routes) > 1) {
         // Custom routing
         self::$rsegments = self::routed_uri(self::$current_uri);
     }
     // The routed URI is now complete
     self::$routed_uri = self::$rsegments;
     // Routed segments will never be empty
     self::$rsegments = explode('/', self::$rsegments);
     // Prepare to find the controller
     $controller_path = '';
     $method_segment = NULL;
     // Paths to search
     $paths = Kohana::include_paths();
     foreach (self::$rsegments as $key => $segment) {
         // Add the segment to the search path
         $controller_path .= $segment;
         $found = FALSE;
         foreach ($paths as $dir) {
             // Search within controllers only
             $dir .= 'controllers/';
             if (is_dir($dir . $controller_path) or is_file($dir . $controller_path . EXT)) {
                 // Valid path
                 $found = TRUE;
                 // The controller must be a file that exists with the search path
                 if ($c = str_replace('\\', '/', realpath($dir . $controller_path . EXT)) and is_file($c) and strpos($c, $dir) === 0) {
                     // Set controller name
                     self::$controller = $segment;
                     // Change controller path
                     self::$controller_path = $c;
                     // Set the method segment
                     $method_segment = $key + 1;
                     // Stop searching
                     break;
                 }
             }
         }
         if ($found === FALSE) {
             // Maximum depth has been reached, stop searching
             break;
         }
         // Add another slash
         $controller_path .= '/';
     }
     if ($method_segment !== NULL and isset(self::$rsegments[$method_segment])) {
         // Set method
         self::$method = self::$rsegments[$method_segment];
         if (isset(self::$rsegments[$method_segment + 1])) {
             // Set arguments
             self::$arguments = array_slice(self::$rsegments, $method_segment + 1);
         }
     }
     // Last chance to set routing before a 404 is triggered
     Event::run('system.post_routing');
     if (self::$controller === NULL) {
         // No controller was found, so no page can be rendered
         Event::run('system.404');
     }
 }
Example #2
0
 /**
  * Router setup routine. Called during the [system.routing][ref-esr]
  * Event by default.
  *
  * [ref-esr]: http://docs.Eightphp.com/events/system.routing
  *
  * @return  boolean
  */
 public static function setup()
 {
     // Set the complete URI
     self::$complete_uri = self::$current_uri . self::$query_string;
     // Load routes
     $routes = Eight::config('routes');
     if (isset($routes['_default']) or count($routes) > 1 and isset($routes[1])) {
         throw new Eight_Exception_User('Routing API Changed!', 'Routing has been significantly changed, and your configuration files are not up to date. ' . 'Please check http://dev.Eightphp.com/changeset/3366 for more details.');
     }
     if (count($routes) > 1) {
         // Get the default route
         $default = $routes['default'];
         // Remove it from the routes
         unset($routes['default']);
         // Add the default route at the end
         $routes['default'] = $default;
     }
     foreach ($routes as $name => $route) {
         // Compile the route into regex
         $regex = Router::compile($route);
         if (preg_match('#^' . $regex . '$#u', self::$current_uri, $matches)) {
             foreach ($matches as $key => $value) {
                 if (is_int($key) or in_array($key, Router::$readonly_keys)) {
                     // Skip matches that are not named or readonly
                     continue;
                 }
                 if ($value !== '') {
                     // Overload the route with the matched value
                     $route[$key] = $value;
                 }
             }
             if (isset($route['prefix'])) {
                 foreach ($route['prefix'] as $key => $prefix) {
                     if (isset($route[$key])) {
                         // Add the prefix to the key
                         $route[$key] = $route['prefix'][$key] . $route[$key];
                     }
                 }
             }
             foreach ($route as $key => $val) {
                 if (is_int($key) or $key === 'controller' or $key === 'method' or in_array($key, self::$readonly_keys)) {
                     // These keys are not arguments, skip them
                     continue;
                 }
                 self::$arguments[$key] = $val;
             }
             // Set controller name
             self::$controller = $route['controller'];
             if (isset($route['method'])) {
                 // Set controller method
                 self::$method = $route['method'];
             } else {
                 // Default method
                 self::$method = 'index';
             }
             // A matching route has been found!
             self::$current_route = $name;
             return TRUE;
         }
     }
     return FALSE;
 }