Пример #1
0
 public static function match(array $requestTypes, $url, $callback)
 {
     $route = new RouterRoute($url, $callback);
     foreach ($requestTypes as $requestType) {
         $route->addRequestType($requestType);
     }
     $router = RouterBase::getInstance();
     $router->addRoute($route);
     return $route;
 }
Пример #2
0
 /**
  * Add a route
  *
  * @param string|array $request_methods
  * @param string $route_name
  * @param string $route_uri
  * @param array $route_config
  *
  * @return RouterRoute
  *
  * @since 1.0.0
  */
 public function route($request_methods, $route_name, $route_uri, array $route_config = [])
 {
     if (isset($this->links[$route_name])) {
         throw new RouterRouteException(sprintf('%s is already defined in route.', $route_name));
     }
     $route = new RouterRoute();
     if (!empty($this->map)) {
         foreach ($this->map as $method => $value) {
             $route->{$method}($value);
         }
     }
     $route->uri($route_uri)->name($route_name)->via($request_methods);
     // assign config array method => value
     foreach ($route_config as $route_method => $route_method_value) {
         if (!method_exists($route, $route_method)) {
             throw new RouterRouteException(sprintf('$route_config[%s] is not a method from %s', $route_method, get_class($route)));
         }
         if ($route_method == 'handler') {
             $route->handler($route_method_value);
         } elseif (is_string($route_method_value)) {
             $route->{$route_method}($route_method_value);
         } elseif (is_array($route_method_value)) {
             call_user_func_array([$route, $route_method], [$route_method_value]);
         } else {
             throw new RouterRouteException(sprintf('$route_config[%s] value must be String|Array instead of "%s"', $route_method, gettype($route_method_value)));
         }
     }
     $this->routes[$route->name] = $route;
     return $route;
 }