Example #1
0
 /**
  * Start the route dispatcher and resolve the URL request.
  * @param  string $URL The URL to match onto.
  * @return boolean true if a route callback was executed.
  */
 public static function dispatch($URL = null)
 {
     if (null === $URL) {
         $URL = Request::URI();
     }
     $method = Request::method();
     foreach ((array) static::$routes as $group => $routes) {
         foreach ($routes as $route) {
             if (false !== ($args = $route->match($URL, $method))) {
                 $route->run($args, $method);
                 return true;
             }
         }
     }
     Response::error(404, '404 Resource not found.');
     Event::trigger(404);
 }
Example #2
0
 /**
  * Start the route dispatcher and resolve the URL request.
  * @param  string $URL The URL to match onto.
  * @param  string $method The HTTP method.
  * @param  bool $return_route If setted to true it will *NOT* execute the route but it will return her.
  * @return boolean true if a route callback was executed.
  */
 public static function dispatch($URL = null, $method = null, $return_route = false)
 {
     if (!$URL) {
         $URL = Request::URI();
     }
     if (!$method) {
         $method = Request::method();
     }
     $__deferred_send = new Deferred(function () {
         if (Options::get('core.response.autosend', true)) {
             Response::send();
         }
     });
     if (empty(static::$optimized_tree)) {
         foreach ((array) static::$routes as $group => $routes) {
             foreach ($routes as $route) {
                 if (is_a($route, 'Route') && $route->match($URL, $method)) {
                     if ($return_route) {
                         return $route;
                     } else {
                         $route->run($route->extractArgs($URL), $method);
                         return true;
                     }
                 }
             }
         }
     } else {
         $routes =& static::$optimized_tree;
         foreach (explode('/', trim($URL, '/')) as $segment) {
             if (is_array($routes) && isset($routes[$segment])) {
                 $routes =& $routes[$segment];
             } else {
                 if (is_array($routes) && isset($routes[''])) {
                     $routes =& $routes[''];
                 } else {
                     break;
                 }
             }
         }
         if (is_array($routes) && isset($routes[0]) && !is_array($routes[0])) {
             foreach ((array) $routes as $route) {
                 if (is_a($route, __CLASS__) && $route->match($URL, $method)) {
                     if ($return_route) {
                         return $route;
                     } else {
                         $route->run($route->extractArgs($URL), $method);
                         return true;
                     }
                 }
             }
         }
     }
     Response::status(404, '404 Resource not found.');
     foreach (array_filter(array_merge(static::trigger(404) ?: [], Event::trigger(404) ?: [])) as $res) {
         Response::add($res);
     }
     return false;
 }