コード例 #1
0
 /**
  *
  */
 public function testGetRoute()
 {
     $request = new Request();
     $this->assertNull($request->getRoute());
     $route = m::mock('mako\\http\\routing\\Route');
     $request->setRoute($route);
     $this->assertSame($route, $request->getRoute());
 }
コード例 #2
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);
     }
 }