/** * Get the route information for a given route. * * @param Route $route * @return array */ protected function getRouteInformation(Route $route) { $result = []; $result['uri'] = $route->getMethods()[0] . ' ' . $route->getPattern(); $result['name'] = $route->getName() ?: ''; $result['group'] = ''; foreach ($route->getGroups() as $group) { $result['group'] .= $group->getPattern(); } $callable = $route->getCallable(); $result['middleware'] = []; foreach ($route->getMiddleware() as $middleware) { $result['middleware'][] = Closure::bind(function () { return get_class($this->callable); }, $middleware, DeferredCallable::class)->__invoke(); } if (is_array($callable)) { $result['callable'] = get_class($callable[0]) . ':' . $callable[1]; $reflector = new ReflectionMethod($callable[0], $callable[1]); } elseif ($callable instanceof Closure) { $result['callable'] = $this->formatVar($callable); $reflector = new ReflectionFunction($callable); } elseif (is_object($callable)) { $result['callable'] = get_class($callable); $reflector = new ReflectionMethod($callable, '__invoke'); } else { $result['callable'] = $callable; $callable = explode(':', $callable); if (isset($callable[1])) { $reflector = new ReflectionMethod($callable[0], $callable[1]); } else { $reflector = new ReflectionMethod($callable, '__invoke'); } } $result['file'] = $reflector->getFileName() . ':' . $reflector->getStartLine() . '-' . $reflector->getEndLine(); return $result; }
/** * Dispatch route * * This method invokes the route object's callable. If middleware is * registered for the route, each callable middleware is invoked in * the order specified. * * @param \Slim\Route $route The route object * @return bool Was route callable invoked successfully? */ public function dispatch(\Slim\Route $route) { $this->currentRoute = $route; //Invoke middleware foreach ($route->getMiddleware() as $mw) { call_user_func_array($mw, array($route)); } //Invoke callable call_user_func_array($route->getCallable(), array_values($route->getParams())); return true; }
/** * Dispatch route * * This method invokes the route object'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 Exception\RequestSlash(); } //Invoke middleware foreach ($route->getMiddleware() as $mw) { if (is_callable($mw)) { call_user_func_array($mw, array($route)); } } //Invoke callable if (is_callable($route->getCallable())) { call_user_func_array($route->getCallable(), array_values($route->getParams())); return true; } return false; }
/** * Override Slim's default `dispatch` function * * @param \Slim\Route $route * * @return bool */ public function dispatch(\Slim\Route $route) { $app = $this->getApp(); $params = $route->getParams(); $callable = $route->getCallable(); // check for a matching autoroute based on the request URI $autoroute = null; if (count($app->routes) > 0) { foreach ($app->routes as $testRoute) { if (!empty($callable) && $callable === $testRoute->getCallback()) { $autoroute = $testRoute; break; } } } // build Request and Response objects to be passed to callable $req = $this->getRequestData($route, $params); $resp = $this->getResponseData(); if (!is_callable($callable)) { return false; } $passParams = $app->config("pass-params") == true; if ($passParams) { // call the autoroute's callback function and pass in the Request and Response objects $result = call_user_func_array($callable, array($req, &$resp)); } else { $app->applyHook("spore.autoroute.before", array("request" => &$req, "response" => &$resp, "autoroute" => &$autoroute)); $result = call_user_func_array($callable, array()); } $outputEmpty = ob_get_length() <= 0; $output = ""; // if the output buffer is empty, we can return our own response if ($outputEmpty) { // if there is no response data, return a blank response if ($result === null && $result !== false) { return true; } if ($autoroute && $autoroute->getTemplate()) { $output = $this->getTemplateOutput($autoroute, $app, $result); } else { $output = Serializer::getSerializedData($app, $result); } if (empty($output)) { return true; } } else { $output = ob_get_clean(); } // return gzip-encoded data if gzip is enabled $gzipEnabled = $app->config("gzip"); $env = $app->environment(); if (substr_count($env["ACCEPT_ENCODING"], "gzip") && extension_loaded("zlib") && $gzipEnabled) { $app->response()->header("Content-Encoding", "gzip"); $app->response()->header("Vary", "Accept-Encoding"); $output = gzencode($output, 9, FORCE_GZIP); } // set the HTTP status $app->status($resp->status); // set the response body $app->response()->body($output); return true; }