/** * */ public function testMethod() { $server = $this->getServerData(); $request = new Request(['server' => $server]); $this->assertEquals('GET', $request->method()); // $request = new Request(['server' => $server, 'method' => 'PATCH']); $this->assertEquals('PATCH', $request->method()); // $server['REQUEST_METHOD'] = 'POST'; $request = new Request(['server' => $server, 'post' => ['REQUEST_METHOD_OVERRIDE' => 'PUT']]); $this->assertEquals('PUT', $request->method()); // $server['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'OPTIONS'; $request = new Request(['server' => $server]); $this->assertEquals('OPTIONS', $request->method()); }
/** * 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); } }