/** * Match method and route with provided routes. * If route and method match a route is dispatch using provided dispatcher. * * @param string $method http method * @param string $url url * * @return mixed */ public function matchRoute($method, $url) { $parsedRoutes = $this->_parseRoutes($this->_routeCollection->getRoutes()); foreach ($parsedRoutes as $parameters) { $matches = $this->_routeMatch($parameters->getParsed(), $url); if ($matches !== false) { $routeMethod = $parameters->getMethod(); if ($routeMethod === $method || $routeMethod === Method::ANY) { return $this->_dispatcher->dispatchRoute($parameters, $matches); } } } return $this->_dispatcher->dispatchNotFound(); }
/** * Test addRoute method * * @return void */ public function testAddRoute() { $testInstance = new RouteCollection(); $route = new Route(Method::ANY, 'test.com/any', function () { }); $testInstance->addRoute($route); $route = new Route(Method::POST, 'test.com/post', function () { }); $testInstance->addRoute($route); $route = new Route(Method::GET, 'test.com/get', function () { }); $testInstance->addRoute($route); $definedRoutes = $testInstance->getRoutes(); list($routeAny, $routePost, $routeGet) = $testInstance->getRoutes(); $this->assertEquals(Method::ANY, $routeAny->getMethod()); $this->assertEquals('test.com/any', $routeAny->getUrl()); $this->assertEquals(Method::POST, $routePost->getMethod()); $this->assertEquals('test.com/post', $routePost->getUrl()); $this->assertEquals(Method::GET, $routeGet->getMethod()); $this->assertEquals('test.com/get', $routeGet->getUrl()); }
/** * Test dynamic controller, dynamic action in module REQUEST * * @return void */ public function testWithoutModeRewriteFullUrl() { $routeCollection = new RouteCollection(); $routeCollection->get('/{module}/{controler}/{action}/{id:\\d+}', ['module' => '{module}', 'controler' => '{controler}', 'action' => '{action}']); $dispatcher = new DispatcherController(); $dispatcher->setBaseNamespace('Test\\Application'); $matcher = new Matcher($routeCollection, $dispatcher); $matcher->setUrlFormat(RouteMode::GET_FORMAT); $enviromentData = ['QUERY_STRING' => 'r=testModule/test/view/123', 'REQUEST_METHOD' => 'GET', 'REQUEST_URI' => '/index.php?r=testModule/test/view/123']; $enviroment = new Request\Enviroment($enviromentData); $videoId = $matcher->match(new Request\Request($enviroment)); $this->assertEquals(123, $videoId); }