/** * 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()); }