/** * @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); }
public function setUp() { $this->router = $router = new TreeRouteStack(); $route = new Segment('/resource[/[:id]]'); $router->addRoute('resource', $route); $route2 = new Segment('/help'); $router->addRoute('docs', $route2); $router->addRoute('hostname', ['type' => 'hostname', 'options' => ['route' => 'localhost.localdomain'], 'child_routes' => ['resource' => ['type' => 'segment', 'options' => ['route' => '/resource[/:id]'], 'may_terminate' => true, 'child_routes' => ['children' => ['type' => 'literal', 'options' => ['route' => '/children']]]], 'users' => ['type' => 'segment', 'options' => ['route' => '/users[/:id]']], 'contacts' => ['type' => 'segment', 'options' => ['route' => '/contacts[/:id]']], 'embedded' => ['type' => 'segment', 'options' => ['route' => '/embedded[/:id]']], 'embedded_custom' => ['type' => 'segment', 'options' => ['route' => '/embedded_custom[/:custom_id]']]]]); $this->event = $event = new MvcEvent(); $event->setRouter($router); $router->setRequestUri(new Http('http://localhost.localdomain/resource')); $controller = $this->controller = $this->getMock('Zend\\Mvc\\Controller\\AbstractRestfulController'); $controller->expects($this->any())->method('getEvent')->will($this->returnValue($event)); $this->urlHelper = $urlHelper = new UrlHelper(); $urlHelper->setRouter($router); $this->serverUrlHelper = $serverUrlHelper = new ServerUrlHelper(); $serverUrlHelper->setScheme('http'); $serverUrlHelper->setHost('localhost.localdomain'); $this->plugin = $plugin = new HalHelper(); $plugin->setController($controller); $plugin->setUrlHelper($urlHelper); $plugin->setServerUrlHelper($serverUrlHelper); $linkExtractor = new LinkExtractor($serverUrlHelper, $urlHelper); $linkCollectionExtractor = new LinkCollectionExtractor($linkExtractor); $plugin->setLinkCollectionExtractor($linkCollectionExtractor); }
/** * @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; }
protected function createRouter() { $route = new RegexRoute('/assets/(?<resourcePath>.*)', '/assets/%resourcePath%'); $router = new TreeRouteStack(); $router->addRoute('asset', $route); return $router; }
public function setUp() { $router = new TreeRouteStack(); $router->addRoute('home', LiteralRoute::factory(array('route' => '/', 'defaults' => array('controller' => 'ZendTest\\Mvc\\Controller\\TestAsset\\SampleController')))); $router->addRoute('sub', SegmentRoute::factory(array('route' => '/foo/:param', 'defaults' => array('param' => 1)))); $router->addRoute('ctl', SegmentRoute::factory(array('route' => '/ctl/:controller', 'defaults' => array('__NAMESPACE__' => 'ZendTest\\Mvc\\Controller\\TestAsset', 'controller' => 'sample')))); $this->controller = new SampleController(); $this->request = new Request(); $this->event = new MvcEvent(); $this->routeMatch = new RouteMatch(array('controller' => 'controller-sample', 'action' => 'postPage')); $this->event->setRequest($this->request); $this->event->setRouteMatch($this->routeMatch); $this->event->setRouter($router); $this->sessionManager = new SessionManager(); $this->sessionManager->destroy(); $this->controller->setEvent($this->event); $plugins = $this->controller->getPluginManager(); $plugins->get('flashmessenger')->setSessionManager($this->sessionManager); }
public function testIsActiveReturnsTrueWhenMatchingRoute() { $page = new Page\Mvc(array('label' => 'spiffyjrwashere', 'route' => 'lolfish')); $route = new LiteralRoute('/lolfish'); $router = new TreeRouteStack(); $router->addRoute('lolfish', $route); $routeMatch = new RouteMatch(array()); $routeMatch->setMatchedRouteName('lolfish'); $page->setRouter($router); $page->setRouteMatch($routeMatch); $this->assertEquals(true, $page->isActive()); }
private function matchUrl($url, $urlHelper) { $url = 'http://localhost.localdomain' . $url; $request = new Request(); $request->setUri($url); $router = new TreeRouteStack(); $router->addRoute('hostname', ['type' => 'hostname', 'options' => ['route' => 'localhost.localdomain'], 'child_routes' => ['resource' => ['type' => 'segment', 'options' => ['route' => '/resource[/:id]']]]]); $match = $router->match($request); if ($match instanceof RouteMatch) { $urlHelper->setRouter($router); $urlHelper->setRouteMatch($match); } return $match; }
public function testHrefGeneratedIsRouteAware() { $page = new Page\Mvc(array('label' => 'foo', 'action' => 'myaction', 'controller' => 'mycontroller', 'route' => 'myroute', 'params' => array('page' => 1337))); $route = new RegexRoute('(lolcat/(?<action>[^/]+)/(?<page>\\d+))', '/lolcat/%action%/%page%', array('controller' => 'foobar', 'action' => 'bazbat', 'page' => 1)); $router = new TreeRouteStack(); $router->addRoute('myroute', $route); $routeMatch = new RouteMatch(array('controller' => 'foobar', 'action' => 'bazbat', 'page' => 1)); $urlHelper = new UrlHelper(); $urlHelper->setRouter($router); $urlHelper->setRouteMatch($routeMatch); $page->setUrlHelper($urlHelper); $page->setRouteMatch($routeMatch); $this->assertEquals('/lolcat/myaction/1337', $page->getHref()); }
private function addRoute($routeName, $route, $routeOptions, $options) { if (isset($options['routeName'])) { $routeName = $options['routeName']; unset($options['routeName']); } if (isset($options['route'])) { $route = $options['route']; unset($options['route']); } $routeOptions = array_merge($routeOptions, $options); $routeSegment = Segment::factory(['route' => $route, 'defaults' => $routeOptions]); $this->router->addRoute($routeName, $routeSegment); }
public function testIsActiveReturnsTrueWhenMatchingRouteWhileUsingModuleRouteListener() { $page = new Page\Mvc(array('label' => 'mpinkstonwashere', 'route' => 'roflcopter', 'controller' => 'index')); $route = new LiteralRoute('/roflcopter'); $router = new TreeRouteStack(); $router->addRoute('roflcopter', $route); $routeMatch = new RouteMatch(array(ModuleRouteListener::MODULE_NAMESPACE => 'Application\\Controller', 'controller' => 'index')); $routeMatch->setMatchedRouteName('roflcopter'); $event = new MvcEvent(); $event->setRouter($router)->setRouteMatch($routeMatch); $moduleRouteListener = new ModuleRouteListener(); $moduleRouteListener->onRoute($event); $page->setRouter($event->getRouter()); $page->setRouteMatch($event->getRouteMatch()); $this->assertEquals(true, $page->isActive()); }
public function testDefaultParamDoesNotOverrideParamForAssembling() { $stack = new TreeRouteStack(); $stack->addRoute('foo', new TestAsset\DummyRouteWithParam()); $stack->setDefaultParam('foo', 'baz'); $this->assertEquals('bar', $stack->assemble(array('foo' => 'bar'), array('name' => 'foo'))); }
public function testCanAppendPreviousUri() { $response = new HttpResponse(); $request = new HttpRequest(); $request->setUri('http://example.com'); $router = new TreeRouteStack(); $router->addRoute('login', ['type' => 'Zend\\Mvc\\Router\\Http\\Literal', 'options' => ['route' => '/login']]); $mvcEvent = new MvcEvent(); $mvcEvent->setParam('exception', new UnauthorizedException()); $mvcEvent->setResponse($response); $mvcEvent->setRequest($request); $mvcEvent->setRouter($router); $options = new RedirectStrategyOptions(['redirect_to_route_disconnected' => 'login', 'append_previous_uri' => true, 'previous_uri_query_key' => 'redirect-uri']); $authenticationService = $this->getMock('Zend\\Authentication\\AuthenticationService'); $authenticationService->expects($this->once())->method('hasIdentity')->will($this->returnValue(false)); $redirectStrategy = new RedirectStrategy($options, $authenticationService); $redirectStrategy->onError($mvcEvent); $this->assertInstanceOf('Zend\\Stdlib\\ResponseInterface', $mvcEvent->getResult()); $this->assertEquals(302, $mvcEvent->getResponse()->getStatusCode()); $this->assertEquals('/login?redirect-uri=http://example.com/', $mvcEvent->getResponse()->getHeaders()->get('Location')->getFieldValue()); }
public function testInheritedRouteMatchParamsWorkWithModuleRouteListener() { $page = new Page\Mvc(array('label' => 'mpinkstonwashere', 'route' => 'lmaoplane')); $route = new SegmentRoute('/lmaoplane[/:controller]'); $router = new TreeRouteStack(); $router->addRoute('lmaoplane', $route); $routeMatch = new RouteMatch(array(ModuleRouteListener::MODULE_NAMESPACE => 'Application\\Controller', 'controller' => 'index')); $routeMatch->setMatchedRouteName('lmaoplane'); $event = new MvcEvent(); $event->setRouter($router)->setRouteMatch($routeMatch); $moduleRouteListener = new ModuleRouteListener(); $moduleRouteListener->onRoute($event); $page->setRouter($event->getRouter()); $page->setRouteMatch($event->getRouteMatch()); $this->assertEquals('/lmaoplane', $page->getHref()); $page->setUseRouteMatch(true); $this->assertEquals('/lmaoplane/index', $page->getHref()); }
public function testChainRouteAssembling() { $stack = new TreeRouteStack(); $stack->addPrototype('bar', array('type' => 'literal', 'options' => array('route' => '/bar'))); $stack->addRoute('foo', array('type' => 'literal', 'options' => array('route' => '/foo'), 'chain_routes' => array('bar'))); $this->assertEquals('/foo/bar', $stack->assemble(array(), array('name' => 'foo'))); }
public function testMistakeDetectIsActiveOnIndexController() { $page = new Page\Mvc(array('label' => 'some Label', 'route' => 'myRoute')); $route = new LiteralRoute('/foo'); $router = new TreeRouteStack(); $router->addRoute('myRoute', $route); $routeMatch = new RouteMatch(array(ModuleRouteListener::MODULE_NAMESPACE => 'Application\\Controller', 'controller' => 'index', 'action' => 'index')); $routeMatch->setMatchedRouteName('index'); $event = new MvcEvent(); $event->setRouter($router)->setRouteMatch($routeMatch); $moduleRouteListener = new ModuleRouteListener(); $moduleRouteListener->onRoute($event); $page->setRouter($event->getRouter()); $page->setRouteMatch($event->getRouteMatch()); $this->assertFalse($page->isActive()); }
public function testChainRouteAssemblingWithChildrenAndSecureScheme() { $stack = new TreeRouteStack(); $uri = new \Zend\Uri\Http(); $uri->setHost('localhost'); $stack->setRequestUri($uri); $stack->addRoute('foo', array('type' => 'literal', 'options' => array('route' => '/foo'), 'chain_routes' => array(array('type' => 'scheme', 'options' => array('scheme' => 'https'))), 'child_routes' => array('baz' => array('type' => 'literal', 'options' => array('route' => '/baz'))))); $this->assertEquals('https://localhost/foo/baz', $stack->assemble(array(), array('name' => 'foo/baz'))); }