/** * This method runs if an exception was thrown by the application. It can be overridden in your own application * class for custom error handling (e.g. you could palm the errors off to Slack for review). * * @param Exception $exception The exception that was thrown. */ protected function onException(Exception $exception) { $response = new Response(Status::INTERNAL_SERVER_ERROR, static::get()->twig->render('@tempest/_errors/500.html', array('exception' => $exception))); $response->send(); }
public function dispatch() { if (!$this->_dispatched) { $dispatcher = \FastRoute\simpleDispatcher(function (RouteCollector $collector) { foreach ($this->_routes as $route) { $collector->addRoute($route->method, $route->uri, $route); } }); $info = $dispatcher->dispatch($this->_request->method, $this->_request->uri); if ($info[0] === Dispatcher::FOUND) { // Successful route match. $this->_request->attachNamed($info[2]); $this->_matched = $info[1]; $respond = true; if (count($this->_matched->middleware) > 0) { foreach ($this->_matched->middleware as $middleware) { if (!$this->instantiateAndCall($this->baseMiddlewareNamespace . ltrim($middleware, '\\'), $this->_request, $this->_response)) { $respond = false; break; } } } if ($respond) { $this->_response->body = $this->instantiateAndCall($this->baseControllerNamespace . ltrim($this->_matched->controller, '\\'), $this->_request, $this->_response); } } if ($info[0] === Dispatcher::NOT_FOUND) { $useTemplate = false; if (!empty($this->_request->uri)) { // Attempt to load HTML file with the same name. // Hitting the root looks for index.html. $template = ($this->_request->uri === '/' ? 'index' : $this->_request->uri) . '.html'; if ($this->_request->method === 'GET' && Tempest::get()->twig->loader->exists($template)) { $useTemplate = true; foreach (explode('/', $template) as $part) { // Don't use templates if it or any ancestor directory begins with an underscore. if (strpos($part, '_') === 0) { $useTemplate = false; break; } } if ($useTemplate) { $this->_response->body = Tempest::get()->twig->render($template); } } } if (!$useTemplate) { $this->_response->status = Status::NOT_FOUND; if (Tempest::get()->twig->loader->exists('404.html')) { $this->_response->body = Tempest::get()->twig->render('404.html'); } else { $this->_response->body = Tempest::get()->twig->render('@tempest/_errors/404.html'); } } } if ($info[0] === Dispatcher::METHOD_NOT_ALLOWED) { $this->_response->status = Status::METHOD_NOT_ALLOWED; $this->_response->body = Tempest::get()->twig->render('@tempest/_errors/405.html'); } $this->_response->send(); } $this->_dispatched = true; }