/** * @param Route $route */ public function addRoute(Route $route) { $path = $route->getPath(); $options = $route->getOptions() ?: []; $options = array_replace_recursive($options, ['route' => $route->getPath(), 'defaults' => ['middleware' => $route->getMiddleware()]]); $spec = ['type' => 'segment', 'options' => $options]; $this->zf2Router->addRoute($path, $spec); $this->routes[$path] = $route; }
/** * Add a route to the collection. * * Uses the HTTP methods associated (creating sane defaults for an empty * list or Route::HTTP_METHOD_ANY) and the path, and uses the path as * the name (to allow later lookup of the middleware). * * @param Route $route */ public function addRoute(Route $route) { $methods = $route->getAllowedMethods(); if ($methods === Route::HTTP_METHOD_ANY) { $methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS', 'TRACE']; } if (empty($methods)) { $methods = ['GET', 'HEAD', 'OPTIONS']; } $this->router->addRoute($methods, $route->getPath(), $route->getPath()); $this->routes[$route->getName()] = $route; }
/** * @param Route $route */ public function addRoute(Route $route) { $name = $route->getName(); $path = $route->getPath(); $options = $route->getOptions(); $options = array_replace_recursive($options, ['route' => $path, 'defaults' => ['middleware' => $route->getMiddleware()]]); $allowedMethods = $route->getAllowedMethods(); if (Route::HTTP_METHOD_ANY === $allowedMethods) { $this->zf2Router->addRoute($name, ['type' => 'segment', 'options' => $options]); $this->routeNameMap[$name] = $name; return; } // Remove the middleware from the segment route in favor of method route unset($options['defaults']['middleware']); if (empty($options['defaults'])) { unset($options['defaults']); } $httpMethodRouteName = implode(':', $allowedMethods); $httpMethodRoute = $this->createHttpMethodRoute($route); $methodNotAllowedRoute = $this->createMethodNotAllowedRoute($path); $spec = ['type' => 'segment', 'options' => $options, 'may_terminate' => false, 'child_routes' => [$httpMethodRouteName => $httpMethodRoute, self::METHOD_NOT_ALLOWED_ROUTE => $methodNotAllowedRoute]]; if (array_key_exists($path, $this->allowedMethodsByPath)) { $allowedMethods = array_merge($this->allowedMethodsByPath[$path], $allowedMethods); // Remove the method not allowed route as it is already present for the path unset($spec['child_routes'][self::METHOD_NOT_ALLOWED_ROUTE]); } $this->zf2Router->addRoute($name, $spec); $this->allowedMethodsByPath[$path] = $allowedMethods; $this->routeNameMap[$name] = sprintf('%s/%s', $name, $httpMethodRouteName); }
/** * Add a route to the underlying router. * * Adds the route to the Aura.Router, using the path as the name, and a * middleware value equivalent to the middleware in the Route instance. * * If HTTP methods are defined (and not the wildcard), they are imploded * with a pipe symbol and added as server REQUEST_METHOD criteria. * * If tokens or values are present in the options array, they are also * added to the router. * * @param Route $route */ public function addRoute(Route $route) { $auraRoute = $this->router->add($route->getPath(), $route->getPath(), $route->getMiddleware()); $httpMethods = $route->getAllowedMethods(); if (is_array($httpMethods)) { $auraRoute->setServer(['REQUEST_METHOD' => implode('|', $httpMethods)]); } foreach ($route->getOptions() as $key => $value) { switch ($key) { case 'tokens': $auraRoute->addTokens($value); break; case 'values': $auraRoute->addValues($value); break; } } }
/** * {@inheritdoc} */ public function addRoute(Route $route) { // TODO allow named parameters through options. // TODO allow usage of :controller, :action, etc. // TODO allow using prefixes if ($this->router->wasMatched()) { throw new Exception\RuntimeException('Route was already matched.'); } // Necessary for phalcon not to alter the original middleware. $middleware = $route->getMiddleware() . '\\MockController::mockAction'; $r = $this->router->add($route->getPath(), $middleware, $route->getAllowedMethods()); $r->setName($route->getName()); }
/** * @param Route $route */ public function addRoute(Route $route) { $slimRoute = new \Slim\Route($route->getPath(), [$this, 'dummyCallable']); $slimRoute->setName($route->getName()); $allowedMethods = $route->getAllowedMethods(); $slimRoute->via($allowedMethods === Route::HTTP_METHOD_ANY ? 'ANY' : $allowedMethods); // Process options $options = $route->getOptions(); if (isset($options['conditions']) && is_array($options['conditions'])) { $slimRoute->setConditions($options['conditions']); } // The middleware is merged with the rest of the route params $params = ['middleware' => $route->getMiddleware()]; if (isset($options['defaults']) && is_array($options['defaults'])) { $params = array_merge($options['defaults'], $params); } $slimRoute->setParams($params); $this->router->map($slimRoute); }
/** * Add a route to the underlying router. * * Adds the route to the Aura.Router, using the path as the name, and a * middleware value equivalent to the middleware in the Route instance. * * If HTTP methods are defined (and not the wildcard), they are imploded * with a pipe symbol and added as server REQUEST_METHOD criteria. * * If tokens or values are present in the options array, they are also * added to the router. * * @param Route $route */ public function addRoute(Route $route) { $path = $route->getPath(); $auraRoute = $this->router->add($route->getName(), $path, $route->getMiddleware()); foreach ($route->getOptions() as $key => $value) { switch ($key) { case 'tokens': $auraRoute->addTokens($value); break; case 'values': $auraRoute->addValues($value); break; } } $allowedMethods = $route->getAllowedMethods(); if (Route::HTTP_METHOD_ANY === $allowedMethods) { return; } $auraRoute->setServer(['REQUEST_METHOD' => implode('|', $allowedMethods)]); if (array_key_exists($path, $this->routes)) { $allowedMethods = array_merge($this->routes[$path], $allowedMethods); } $this->routes[$path] = $allowedMethods; }
public function testRoutePathIsRetrievable() { $route = new Route('/foo', $this->noopMiddleware); $this->assertEquals('/foo', $route->getPath()); }
/** * Injects route into the underlying router implemetation. * * @param Route $route */ protected function injectRoute(Route $route) { $pathParser = new Segment('/', $route->getPath(), []); $options = $route->getOptions(); $defaults = isset($options['defaults']) ? $options['defaults'] : []; $methods = Route::HTTP_METHOD_ANY === $route->getAllowedMethods() ? null : $route->getAllowedMethods(); $this->routeCollection->add($route->getName(), new Generic($pathParser, null, $methods, null, null, $defaults)); }