/** * Add trailing slash route or add route without trailing slash so both work * @param string $httpMethod * @param string $route * @param RouteHandler $handler */ public function addRoute($httpMethod, $route, $handler) { parent::addRoute($httpMethod, $route, $handler); // Route ended with slash , redirect non slash to slash if (substr($route, -1) === '/') { parent::addRoute($httpMethod, substr($route, 0, -1), new RouteHandler(function () { $url = tiga_url($this->request->getPathInfo() . "/"); $response = \Tiga\Framework\Response\ResponseFactory::redirect($url); $response->sendHeaders(); die; })); } else { $route .= "/"; parent::addRoute($httpMethod, $route, new RouteHandler(function () { $url = rtrim(tiga_url($this->request->getPathInfo()), '/'); $response = \Tiga\Framework\Response\ResponseFactory::redirect($url); $response->sendHeaders(); die; })); } }
/** * Send response. * * @param RouteHandler $routeHandler * @param callable $vars */ protected function sendResponse(routeHandler $routeHandler, $vars) { // Start buffering, handle any echo-ed content on routeHandler ob_start(); // Handle request $response = $this->handle($routeHandler->getHandler(), $vars); // Transfer buffer to view $content = ob_get_contents(); ob_end_clean(); // Check if the response is Instance or SubClass of Symfony Response if ($response instanceof SymfonyResponse || is_subclass_of($response, 'Symfony\\Component\\HttpFoundation\\Response')) { // Set Buffer to View $this->view->setBuffer($content); } else { // Set status header to 200 $response = ResponseFactory::content($content); } $this->header->setResponse($response); $this->view->setResponse($response); // JSON Response if ($response->isJson() || $routeHandler->isFastExit()) { $response->sendHeaders(); $this->view->render(); die; } }