/** * Execute the middleware. Don't call this method directly; it is used by the `Application` internally. * * @internal * * @param ServerRequestInterface $request The request object * @param ResponseInterface $response The response object * @param callable $next The next middleware handler * * @return ResponseInterface */ public function handle(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) { $attributes = $request->getAttributes(); if (!isset($attributes['command'])) { try { /** @var RepositoryInterface $repository */ $repository = $this->container->get('Repository')->forEntity(Page::class); /** @var Page[] $pages */ $pages = $repository->getAll(); $router = new Router(); foreach ($pages as $page) { $router->get($this->expandUrl($page->url, $page), function () use($page) { return $page; }); } $path = preg_replace('~^/.*?index.php/?~', '', $request->getUri()->getPath()); $route = $router->parseRoute($path); $page = $route['controller'](); $vars = $route['vars']; $command = new DisplayPageCommand($page->id, $vars, $request, $response->getBody(), $this->container); $request = $request->withAttribute('command', $command); // @todo Emit afterRouting event } catch (InvalidArgumentException $e) { // Do nothing } } return $next($request, $response); }
/** * Find and execute the appropriate controller based on a given route. * * @param string $route The route string for which to find and execute a controller. * * @return AbstractTrackerController * * @since 1.0 * @throws RoutingException */ public function getController($route) { try { return parent::getController($route); } catch (\InvalidArgumentException $e) { // 404 throw new RoutingException($e->getMessage()); } catch (\RuntimeException $e) { // 404 throw new RoutingException($e->getMessage()); } }
/** * Constructor. * * @param Input $input An optional input object from which to derive the route. If none * is given than the input from the application object will be used. * @param AbstractApplication $app An optional application object to inject to controllers * * @since 1.0 */ public function __construct(Input $input = null, AbstractApplication $app = null) { parent::__construct($app->input); $this->app = $app; }
/** * Tests the Joomla\Router\Router::fetchController method with a prefix set. * * @return void * * @covers Joomla\Router\Router::fetchController * @since 1.0 */ public function testFetchControllerWithPrefixSet() { $this->instance->setControllerPrefix('MyTestController'); $controller = TestHelper::invoke($this->instance, 'fetchController', 'Foo'); }
/** * Setup the router with routes. * * @return void */ protected function setRoutes() { $this->instance->addRoutes([['pattern' => 'login', 'controller' => 'LoginController'], ['pattern' => 'logout', 'controller' => 'LogoutController'], ['pattern' => 'articles', 'controller' => 'ArticlesController'], ['pattern' => 'articles/:article_id', 'controller' => 'ArticleController'], ['pattern' => 'test/:seg1/path/:seg2', 'controller' => 'TestController'], ['pattern' => 'content/:/\\*', 'controller' => 'ContentController'], ['pattern' => 'content/*category/:article', 'controller' => 'ArticleController'], ['pattern' => '/', 'controller' => 'DefaultController']]); }