Пример #1
0
 /**
  *
  */
 public function testNestedGroup()
 {
     $routes = new Routes();
     $routes->group(['prefix' => 'baz'], function ($routes) {
         $routes->group(['prefix' => 'bax'], function ($routes) {
             $routes->get('/foo', 'FooController::fooAction');
             $routes->get('/bar', 'FooController::barAction');
         });
     });
     $routes = $routes->getRoutes();
     $this->assertEquals('/baz/bax/foo', $routes[0]->getRoute());
     $this->assertEquals('/baz/bax/bar', $routes[1]->getRoute());
 }
Пример #2
0
 /**
  * Returns the URL of a named route.
  *
  * @access  public
  * @param   string  $routeName    Route name
  * @param   array   $routeParams  Route parameters
  * @param   array   $queryParams  Associative array used to build URL-encoded query string
  * @param   string  $separator    Argument separator
  * @param   mixed   $language     Request language
  * @return  string
  */
 public function toRoute($routeName, array $routeParams = [], array $queryParams = [], $separator = '&', $language = true)
 {
     $route = $this->routes->getNamedRoute($routeName)->getRoute();
     foreach ($routeParams as $key => $value) {
         if (!empty($value)) {
             $route = preg_replace('/{' . $key . '}\\??/', $value, $route, 1);
         }
     }
     if (strpos($route, '?') !== false) {
         $route = preg_replace('/\\/{\\w+}\\?/', '', $route);
     }
     return $this->to($route, $queryParams, $separator, $language);
 }
Пример #3
0
 /**
  * Matches and returns the appropriate route along with its parameters.
  *
  * @access  public
  * @param   \mako\http\Request  $request  Request
  * @return  array
  */
 public function route(Request $request)
 {
     $matched = false;
     $parameters = [];
     $requestMethod = $request->method();
     $requestPath = $request->path();
     foreach ($this->routes->getRoutes() as $route) {
         if ($this->matches($route, $requestPath, $parameters)) {
             if (!$route->allows($requestMethod)) {
                 $matched = true;
                 continue;
             }
             // Redirect to URL with trailing slash if the route should have one
             if ($route->hasTrailingSlash() && !empty($requestPath) && substr($requestPath, -1) !== '/') {
                 return [$this->redirectRoute($requestPath), []];
             }
             // If this is an "OPTIONS" request then well collect all the allowed request methods
             // from all routes matching the requested path. We'll then add an "allows" header
             // to the matched route
             if ($requestMethod === 'OPTIONS') {
                 return [$this->optionsRoute($requestPath), []];
             }
             // Assign the route to the request
             $request->setRoute($route);
             // Return the matched route and parameters
             return [$route, $parameters];
         }
     }
     if ($matched) {
         // We found a matching route but it does not allow the request method so we'll throw a 405 exception
         throw new MethodNotAllowedException($this->getAllowedMethodsForMatchingRoutes($requestPath));
     } else {
         // No routes matched so we'll throw a 404 exception
         throw new NotFoundException($requestMethod . ': ' . $requestPath);
     }
 }