/** * Create and return the router * * Retrieves the "router" key of the Config service, and uses it * to instantiate the router. Uses the TreeRouteStack implementation by * default. * * @param ServiceLocatorInterface $serviceLocator * @param string|null $cName * @param string|null $rName * @return \Zend\Mvc\Router\RouteStackInterface */ public function createService(ServiceLocatorInterface $serviceLocator, $cName = null, $rName = null) { $config = $serviceLocator->get('Config'); $routePluginManager = $serviceLocator->get('RoutePluginManager'); if ($rName === 'ConsoleRouter' || $cName === 'router' && Console::isConsole()) { // We are in a console, use console router. if (isset($config['console']) && isset($config['console']['router'])) { $routerConfig = $config['console']['router']; } else { $routerConfig = array(); } $router = new ConsoleRouter($routePluginManager); } else { // This is an HTTP request, so use HTTP router $router = new HttpRouter($routePluginManager); $routerConfig = isset($config['router']) ? $config['router'] : array(); } if (isset($routerConfig['route_plugins'])) { $router->setRoutePluginManager($routerConfig['route_plugins']); } if (isset($routerConfig['routes'])) { $router->addRoutes($routerConfig['routes']); } if (isset($routerConfig['default_params'])) { $router->setDefaultParams($routerConfig['default_params']); } return $router; }
public function setUpRouter() { if (isset($this->router)) { return; } $this->setUpRequest(); $routes = ['resource' => ['type' => 'Segment', 'options' => ['route' => '/api/resource[/:id]', 'defaults' => ['controller' => 'Api\\RestController']]]]; $this->router = $router = new TreeRouteStack(); $router->addRoutes($routes); $matches = $router->match($this->request); if (!$matches instanceof RouteMatch) { $this->fail('Failed to route!'); } $this->matches = $matches; }
public function testPriorityIsPassedToPartRoute() { $stack = new TreeRouteStack(); $stack->addRoutes(array('foo' => array('type' => 'Literal', 'priority' => 1000, 'options' => array('route' => '/foo', 'defaults' => array('controller' => 'foo')), 'may_terminate' => true, 'child_routes' => array('bar' => array('type' => 'Literal', 'options' => array('route' => '/bar', 'defaults' => array('controller' => 'foo', 'action' => 'bar'))))))); $reflectedClass = new \ReflectionClass($stack); $reflectedProperty = $reflectedClass->getProperty('routes'); $reflectedProperty->setAccessible(true); $routes = $reflectedProperty->getValue($stack); $this->assertEquals(1000, $routes->get('foo')->priority); }
public function setUpAlternateRouter() { $routes = array('parent' => array('type' => 'Segment', 'options' => array('route' => '/api/parent[/:id]', 'defaults' => array('controller' => 'Api\\ParentController')), 'may_terminate' => true, 'child_routes' => array('child' => array('type' => 'Segment', 'options' => array('route' => '/child[/:child_id]', 'defaults' => array('controller' => 'Api\\ChildController')))))); $this->router = $router = new TreeRouteStack(); $router->addRoutes($routes); $this->helpers->get('url')->setRouter($router); }
/** * Sets up the router based on the configuration provided * * @param Application $application * @return void */ protected function setupRouter(AppContext $application) { $router = new Router(); $router->addRoutes($this->config->routes); $application->setRouter($router); }
public function testRecursiveDetectIsActiveWhenRouteNameIsKnown() { $parentPage = new Page\Mvc(array('label' => 'some Label', 'route' => 'parentPageRoute')); $childPage = new Page\Mvc(array('label' => 'child', 'route' => 'childPageRoute')); $parentPage->addPage($childPage); $router = new TreeRouteStack(); $router->addRoutes(array('parentPageRoute' => array('type' => 'literal', 'options' => array('route' => '/foo', 'defaults' => array('controller' => 'fooController', 'action' => 'fooAction'))), 'childPageRoute' => array('type' => 'literal', 'options' => array('route' => '/bar', 'defaults' => array('controller' => 'barController', 'action' => 'barAction'))))); $routeMatch = new RouteMatch(array(ModuleRouteListener::MODULE_NAMESPACE => 'Application\\Controller', 'controller' => 'barController', 'action' => 'barAction')); $routeMatch->setMatchedRouteName('childPageRoute'); $event = new MvcEvent(); $event->setRouter($router)->setRouteMatch($routeMatch); $moduleRouteListener = new ModuleRouteListener(); $moduleRouteListener->onRoute($event); $parentPage->setRouter($event->getRouter()); $parentPage->setRouteMatch($event->getRouteMatch()); $childPage->setRouter($event->getRouter()); $childPage->setRouteMatch($event->getRouteMatch()); $this->assertTrue($childPage->isActive(true)); $this->assertTrue($parentPage->isActive(true)); }