Example #1
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;
 }