app = $app; } /* Custom error handlers registered with error() take precedence over the built-in error handler provider by Silex, but the formatted error messages it provides can be accessed in debug mode by returning based on $app['debug'] like this. */ public function __invoke(\Exception $e, Request $request, $code) { // Use the default handler in debug mode. if ($this->app['debug']) { return; } // Use our error formats otherwise. // 404.html, or 40x.html, or 4xx.html, or error.html $templates = [ 'errors/' . $code . '.html.twig', 'errors/' . substr($code, 0, 2) . 'x.html.twig', 'errors/' . substr($code, 0, 1) . 'xx.html.twig', 'errors/default.html.twig', ]; /** @var \Twig_Environment $twig */ $twig = $this->app['twig']; /** @var \Twig_Template $template */ $template = $twig->resolveTemplate($templates); return new Response($template->render(['code' => $code . ' : ' . $e->getMessage()])); } }