/** * Dispatch route * * This method invokes the route's callable. If middleware is * registered for the route, each callable middleware is invoked in * the order specified. * * This method is smart about trailing slashes on the route pattern. * If the route's pattern is defined with a trailing slash, and if the * current request URI does not have a trailing slash but otherwise * matches the route's pattern, a Slim_Exception_RequestSlash * will be thrown triggering an HTTP 301 Permanent Redirect to the same * URI _with_ a trailing slash. This Exception is caught in the * `Slim::call` loop. If the route's pattern is defined without a * trailing slash, and if the current request URI does have a trailing * slash, the route will not be matched and a 404 Not Found * response will be sent if no subsequent matching routes are found. * * @param Slim_Route $route The route object * @return bool Was route callable invoked successfully? * @throws Slim_Exception_RequestSlash */ public function dispatch(Slim_Route $route) { if (substr($route->getPattern(), -1) === '/' && substr($this->resourceUri, -1) !== '/') { throw new Slim_Exception_RequestSlash(); } //Invoke middleware foreach ($route->getMiddleware() as $mw) { if (is_callable($mw)) { call_user_func($mw); } } //Invoke callable if (is_callable($route->getCallable())) { call_user_func_array($route->getCallable(), array_values($route->getParams())); return true; } return false; }
/** * Route does not match URI with wildcard */ public function testRouteDoesNotMatchResourceWithWildcard() { $resource = '/hello'; $route = new Slim_Route('/hello/:path+', function () { }); $result = $route->matches($resource); $this->assertFalse($result); $this->assertEquals(array(), $route->getParams()); }
/** * Route optional parameters * * Pre-conditions: * Route pattern requires :year, optionally accepts :month and :day * * Post-conditions: * All: Year is 2010 * Case A: Month and day default values are used * Case B: Month is "05" and day default value is used * Case C: Month is "05" and day is "13" */ public function testRouteOptionalParameters() { $pattern = '/archive/:year(/:month(/:day))'; //Case A $routeA = new Slim_Route($pattern, function () { }); $resourceA = '/archive/2010'; $resultA = $routeA->matches($resourceA); $this->assertTrue($resultA); $this->assertEquals(array('year' => '2010'), $routeA->getParams()); //Case B $routeB = new Slim_Route($pattern, function () { }); $resourceB = '/archive/2010/05'; $resultB = $routeB->matches($resourceB); $this->assertTrue($resultB); $this->assertEquals(array('year' => '2010', 'month' => '05'), $routeB->getParams()); //Case C $routeC = new Slim_Route($pattern, function () { }); $resourceC = '/archive/2010/05/13'; $resultC = $routeC->matches($resourceC); $this->assertTrue($resultC); $this->assertEquals(array('year' => '2010', 'month' => '05', 'day' => '13'), $routeC->getParams()); }