/** * Route sets custom pattern that overrides pattern */ public function testRouteSetsCustomTemplate() { $route = new Slim_Route('/hello/*', 'hello'); $route->setPattern('/hello/:name'); $this->assertEquals('/hello/:name', $route->getPattern()); }
/** * Route should set pattern */ public function testRouteSetsPattern() { $route1 = new Slim_Route('/foo/bar', function () { }); $this->assertEquals('/foo/bar', $route1->getPattern()); }
/** * 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 should set pattern, and the Route pattern should not * retain the leading slash. */ public function testRouteSetsPatternWithoutLeadingSlash() { $route = new Slim_Route('/foo/bar', function () { }); $this->assertEquals('/foo/bar', $route->getPattern()); }