/** * */ public function testRouteRegistration1() { $routes = new Routes(); $routes->get('/foo', 'FooController::fooAction'); $this->assertCount(1, $routes->getRoutes()); $routes->get('/bar', 'FooController::barAction'); $this->assertCount(2, $routes->getRoutes()); }
/** * 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); } }