public static function notAuthorizedRoutesFunction(\FastRoute\RouteCollector $r) { $r->addRoute('GET', Route::oauthStart(), 'GithubExample\\Controller\\Controller::oauthStart'); $r->addRoute('GET', Route::oauthConfirm(), 'GithubExample\\Controller\\Controller::oauthConfirm'); $r->addRoute('GET', Route::oauthReturn(), 'GithubExample\\Controller\\Controller::oauthReturn'); self::commonRoutesFunction($r); }
public static function routesFunction(\FastRoute\RouteCollector $r) { $r->addRoute('GET', '/', ['TierJigSkeleton\\Controller\\Basic', 'index']); $r->addRoute('GET', '/notepad', ['TierJigSkeleton\\Controller\\Basic', 'notepad']); $r->addRoute('GET', '/login', ['TierJigSkeleton\\Controller\\Basic', 'login']); $r->addRoute('POST', '/login', ['TierJigSkeleton\\Controller\\Basic', 'loginProcess']); $r->addRoute('POST', '/testBadMethod', ['TierJigSkeleton\\Controller\\Index', 'testBadMethod']); }
/** * @inheritDoc */ public function parse(array $routes) : array { /** @var \Venta\Contracts\Routing\Route $route */ foreach ($routes as $route) { $this->collector->addRoute($route->getMethods(), $route->getPath(), $route); } return $this->collector->getData(); }
/** * 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; }
public function __invoke(RouteCollector $router) { $routes = $this->collection->all(); foreach ($routes as $name => $route) { $router->addRoute($route->getMethods(), $route->getPath(), $name); } }
/** * @param string|string[] $methods * @param string $path * @param callable $handler * @return Route */ public function route($methods, $path, $handler) { $route = $this->routeMap->add($path, $handler); $this->collector->addRoute($methods, $path, $route); $this->middleware[] = $route; return $route; }
protected function buildRules() { $this->routeCollector = new RouteCollector($this->getParser(), new GroupCountBased()); foreach ($this->rules as $rule) { $this->routeCollector->addRoute($rule->getMethod(), $rule->getPattern(), $rule->getRoute()); } }
function it_parses_routes(RouteCollector $collector, Route $route) { $route->getMethods()->willReturn(['GET']); $route->getPath()->willReturn('/url'); $collector->addRoute(['GET'], '/url', $route)->shouldBeCalled(); $collector->getData()->willReturn(['parse result']); $this->parse([$route])->shouldBe(['parse result']); }
/** * Added restful routes. * * @param RouteCollector $route * @param array $param */ private function addRestfulMethods($route, $param) { $uri = $param[1]; $class = $param[2][0]; $methods = ['index' => ['GET', $uri], 'read' => ['GET', $uri . '/{id}'], 'create' => ['POST', $uri], 'update' => ['PUT', $uri . '/{id}'], 'delete' => ['DELETE', $uri . '/{id}']]; foreach ($methods as $key => $method) { $route->addRoute($method[0], $method[1], [$class, $key]); } }
public function init() { $collector = new RouteCollector(new StdRouteParser(), new GroupCountBasedDataGenerator()); foreach ($this->routes as $route) { foreach ($route->definition($this->container) as list($method, $route_name, $handler_name)) { $collector->addRoute($method, $route_name, $handler_name); } } $this->dispatcher = new GroupCountBasedDispatcher($collector->getData()); }
/** * @param \ReflectionMethod $method * @param string $controllerPath * @throws RouterConfigurationException */ private function configureControllerMethod(\ReflectionMethod $method, $controllerPath) { /** @var HttpVerb $httpVerbAnnotation */ $httpVerbAnnotation = null; $annotations = Annotations::ofMethod($method); foreach ($annotations as $annotation) { if (in_array(get_class($annotation), $this->httpVerbsAnnotations)) { $httpVerbAnnotation = $annotation; break; } } if ($httpVerbAnnotation === null) { return; } $methodPath = $httpVerbAnnotation->path; $fullPath = $this->basePath . $controllerPath . $methodPath; $methodFullName = $method->getDeclaringClass()->getName() . '::' . $method->getName(); $httpVerb = $this->httpVerbsByAnnotations[get_class($httpVerbAnnotation)]; $this->routeCollector->addRoute($httpVerb, $fullPath, $methodFullName); }
/** * @param array $methods * @param string $path * @param string $className * @param string $methodName * @param array|string $middlewares * @return \Wandu\Router\Route */ public function createRoute(array $methods, $path, $className, $methodName = 'index', array $middlewares = []) { $path = '/' . trim($this->prefix . $path, '/'); if (!is_array($middlewares)) { $middlewares = [$middlewares]; } $middlewares = array_merge($this->middlewares, $middlewares); $handler = implode(',', $methods) . $path; $this->collector->addRoute($methods, $path, $handler); return $this->routes[$handler] = new Route($className, $methodName, $middlewares); }
private function addRoutes(FastRoute\RouteCollector $route) { foreach ($this->routes as $definedRoute) { $definedRoute[3] = isset($definedRoute[3]) ? $definedRoute[3] : $this->defaultReturnType; $route->addRoute(strtoupper($definedRoute[0]), $definedRoute[1], function ($args) use($definedRoute) { $returnType = $definedRoute[3]; $controller = $definedRoute[2]; list($definedRoute, $action) = explode("/", $controller); return ['definedRoute' => $definedRoute, 'action' => $action, 'returnType' => $returnType, 'args' => $args]; }); } }
/** * Add route to route collector * * @param string $swaggerJson Swagger json file path (can be an URL) * @param RouteCollector $routeCollector FastRoute route collector * @param OperationParserInterface $operationParser Swagger operation parser. * @param array $options Options (@see config/swaggerConfig.dist.php) */ function addRoutes($swaggerJson, RouteCollector $routeCollector, OperationParserInterface $operationParser, $options = []) { if (!isset($options['routeFile']) || !file_exists($options['routeFile'])) { $routes = scan($swaggerJson, $operationParser); } else { $routes = (require $options['routeFile']); } if (isset($options['routeFile']) && isset($options['cacheEnabled']) && $options['cacheEnabled']) { cacheRoutes($routes, $options['routeFile']); } foreach ($routes as $route) { $routeCollector->addRoute($route['method'], $route['uri'], $route['action']); } }
/** * {@inheritdoc} */ public function addRoute($methods, $path, $controller, $name = null, array $options = []) { if ($this->isCaching()) { return null; } $methods = (array) $methods; $path = $this->makePath($path); $route = $this->createRoute($methods, $path, $controller, $name, $options); $this->routes->attach($route); if ($name) { $this->addNamedRoute($name, $route); } $this->routeCollector->addRoute($route->getMethods(), $path, $route); return $route; }
/** * Add a route to the collection * * @param string $method * @param string $route * @param string|\Closure $handler * @param integer $strategy * @return \Orno\Route\RouteCollection */ public function addRoute($method, $route, $handler, $strategy = self::REQUEST_RESPONSE_STRATEGY) { // are we running a single strategy for the collection? $strategy = isset($this->strategy) ? $this->strategy : $strategy; // if the handler is an anonymous function, we need to store it for later use // by the dispatcher, otherwise we just throw the handler string at FastRoute if ($handler instanceof \Closure) { $callback = $handler; $handler = uniqid('orno::route::', true); $this->routes[$handler]['callback'] = $callback; } $this->routes[$handler]['strategy'] = $strategy; $route = $this->parseRouteString($route); parent::addRoute($method, $route, $handler); return $this; }
/** * Add a route to the collection * * @param string|string[] $method * @param string $route * @param string|\Closure $handler * @param \League\Route\Strategy\StrategyInterface $strategy * @return \League\Route\RouteCollection */ public function addRoute($method, $route, $handler, Strategy\StrategyInterface $strategy = null) { // are we running a single strategy for the collection? $strategy = !is_null($this->strategy) ? $this->strategy : $strategy; // if the handler is an anonymous function, we need to store it for later use // by the dispatcher, otherwise we just throw the handler string at FastRoute if (!is_string($handler) && is_callable($handler)) { $callback = $handler; $handler = uniqid('league::route::', true); $this->routes[$handler]['callback'] = $callback; } elseif (is_object($handler)) { throw new \RuntimeException('Object controllers must be callable.'); } $this->routes[$handler]['strategy'] = is_null($strategy) ? new Strategy\RequestResponseStrategy() : $strategy; $route = $this->parseRouteString($route); parent::addRoute($method, $route, $handler); return $this; }
public function addRoute($httpMethod, $route, $handler) { if ($httpMethod == 'REST') { // Make REST methods available // TODO: find a better way to do this witout duplicates with an appended slash character parent::addRoute('GET', $route[0], array($handler, 'index')); parent::addRoute('GET', $route[0] . '/', array($handler, 'index')); parent::addRoute('POST', $route[0] . '', array($handler, 'create')); parent::addRoute('POST', $route[0] . '/', array($handler, 'create')); parent::addRoute('GET', $route[0] . '/' . $route[1], array($handler, 'show')); parent::addRoute('GET', $route[0] . '/' . $route[1] . '/', array($handler, 'show')); parent::addRoute('PUT', $route[0] . '/' . $route[1], array($handler, 'edit')); parent::addRoute('PUT', $route[0] . '/' . $route[1] . '/', array($handler, 'edit')); parent::addRoute('PATCH', $route[0] . '/' . $route[1], array($handler, 'edit')); parent::addRoute('PATCH', $route[0] . '/' . $route[1] . '/', array($handler, 'edit')); parent::addRoute('DELETE', $route[0] . '/' . $route[1], array($handler, 'delete')); parent::addRoute('DELETE', $route[0] . '/' . $route[1] . '/', array($handler, 'delete')); return; } parent::addRoute($httpMethod, $route, $handler); }
/** * Add trailing slash route or add route without trailing slash so both work * @param string $httpMethod * @param string $route * @param RouteHandler $handler */ public function addRoute($httpMethod, $route, $handler) { parent::addRoute($httpMethod, $route, $handler); // Route ended with slash , redirect non slash to slash if (substr($route, -1) === '/') { parent::addRoute($httpMethod, substr($route, 0, -1), new RouteHandler(function () { $url = tiga_url($this->request->getPathInfo() . "/"); $response = \Tiga\Framework\Response\ResponseFactory::redirect($url); $response->sendHeaders(); die; })); } else { $route .= "/"; parent::addRoute($httpMethod, $route, new RouteHandler(function () { $url = rtrim(tiga_url($this->request->getPathInfo()), '/'); $response = \Tiga\Framework\Response\ResponseFactory::redirect($url); $response->sendHeaders(); die; })); } }
protected function getCollector() { $collector = new RouteCollector(new Std(), new GroupCountBased2()); $this->routesIndex = []; foreach ($this->routes->getGroups() as $group) { foreach ($group->getRoutes() as $route) { $uri = $route->getRoute(); $key = implode('_', (array) $route->getHttpMethod()) . '_' . $uri; if (!isset($this->routesIndex[$key])) { $collector->addRoute($route->getHttpMethod(), $uri, $key); } $this->routesIndex[$key][] = $route; } } foreach ($this->routes->getRoutes() as $route) { $uri = $route->getRoute(); $key = implode('_', (array) $route->getHttpMethod()) . '_' . $uri; if (!isset($this->routesIndex[$key])) { $collector->addRoute($route->getHttpMethod(), $uri, $key); } $this->routesIndex[$key][] = $route; } return $collector; }
/** * @param string|string[] $methods * @param string $path * @param callable $handler */ public function route($methods, $path, $handler) { $this->collector->addRoute($methods, $path, $handler); }
<?php use FastRoute\RouteCollector; use FastRoute\RouteParser\Std as RouteParser; use FastRoute\DataGenerator\GroupCountBased as RouteGenerator; use FastRoute\Dispatcher\GroupCountBased as Dispatcher; $r = new RouteCollector(new RouteParser(), new RouteGenerator()); $r->addRoute('GET', '/', ['ACME\\Controller\\Index', 'index']); $r->addRoute('GET', '/about[/{name}]', ['ACME\\Controller\\Index', 'about']); return new Dispatcher($r->getData());
public function it_should_able_to_run(ContainerInterface $container, RouteCollector $collector, ServerInterface $server, EventDispatcherInterface $event) { /** * predictions */ $container->offsetSet('Middlewares', [])->shouldBeCalled(); $container->offsetSet('ServiceProviders', [])->shouldBeCalled(); $container->offsetSet('Routes', [])->shouldBeCalled(); $collector->addRoute('GET', '/', 'test')->shouldBeCalled(); $server->run()->shouldBeCalled(); /** * mocks */ $container->offsetGet('Routes')->willReturn([['method' => 'GET', 'route' => '/', 'handler' => 'test']]); $container->offsetGet('Server')->willReturn($server); $container->offsetGet('RouteCollector')->willReturn($collector); $container->offsetGet('EventDispatcher')->willReturn($event); /** * test method */ $this->run(); }
/** * Collect routes from map. * * @param RouteCollector $collector */ public function map(RouteCollector $collector) { foreach ($this->getRoutes() as $row) { $collector->addRoute($row[0], $row[1], $row[2]); } }
function routesFunction(\FastRoute\RouteCollector $r) { $categories = '{category:Imagick|ImagickDraw|ImagickPixel|ImagickPixelIterator|ImagickKernel|Tutorial}'; //Category indices $r->addRoute('GET', "/{$categories}", ['ImagickDemo\\Controller\\Page', 'renderCategoryIndex']); //Category + example $r->addRoute('GET', "/{$categories}/{example:[a-zA-Z]+}", ['ImagickDemo\\Controller\\Page', 'renderExamplePage']); //Images $r->addRoute('GET', "/imageStatus/{$categories}/{example:[a-zA-Z]+}", ['ImagickDemo\\Controller\\Image', 'getImageJobStatus']); //Images $r->addRoute('GET', "/image/{$categories}/{example:[a-zA-Z]+}", ['ImagickDemo\\Controller\\Image', 'getImageResponse']); //Original image $r->addRoute('GET', "/imageOriginal/{$categories}/{example:[a-zA-Z]+}", ['ImagickDemo\\Controller\\Image', 'getOriginalImage']); //Custom images $r->addRoute('GET', "/customImage/{$categories}/{example:[a-zA-Z]*}", ['ImagickDemo\\Controller\\Image', 'getCustomImageResponse']); $r->addRoute('GET', '/info', ['ImagickDemo\\Controller\\ServerInfo', 'createResponse']); $r->addRoute('GET', '/queueinfo', ['ImagickDemo\\Controller\\QueueInfo', 'createResponse']); $r->addRoute('GET', '/queuedelete', ['ImagickDemo\\Controller\\QueueInfo', 'deleteQueue']); $r->addRoute('GET', '/opinfo', ['ImagickDemo\\Controller\\ServerInfo', 'renderOPCacheInfo']); $r->addRoute('GET', '/settingsCheck', ['ImagickDemo\\Controller\\ServerInfo', 'serverSettings']); $r->addRoute('GET', '/', ['ImagickDemo\\Controller\\Page', 'renderTitlePage']); $r->addRoute('GET', "/css/{cssInclude}", ['ScriptServer\\Controller\\ScriptServer', 'getPackedCSS']); $r->addRoute('GET', '/js/{jsInclude}', ['ScriptServer\\Controller\\ScriptServer', 'getPackedJavascript']); }
function routesFunction(\FastRoute\RouteCollector $r) { $r->addRoute('GET', '/introduction', ['TierTest\\Controller\\BasicController', 'introduction']); }
public function addParser(string $method, string $path, string $httpRequestParser) : ApplicationServiceProvider { $this->routeCollector->addRoute($method, $this->routeRoot . $path, $httpRequestParser); return $this; }