TwigErrorRenderer
is a Slim
ErrorRenderer
that renders errors using Twig templates.
You can specify a separate Twig template for each Exception
along with a
default catch-all template. It is recommended to only enable TwigErrorRenderer
in production.
TwigErrorRenderer
is automatically configured for Reun PHP App Template in the
included definitions. Just include the definitions to enable it in production.
// AppDefinitions.php
$c += TwigUtilitiesDefinitions::getDefinitions();
// Default error template
$defaultTemplate = "public/errorPages/default.twig";
$templates = [
// Render Twig loader errors as 404
LoaderError::class => "public/errorPages/404.twig",
// Render Slim's NotFoundException as 404
HttpNotFoundException::class => "public/errorPages/404.twig",
];
$twigErrorRenderer = new TwigErrorRenderer($twig, $defaultTemplate, $templates);
$app
->addErrorMiddleware($isDev, true, true)
->getDefaultErrorHandler()
->registerErrorRenderer("text/html", $twigErrorRenderer);
See the included definitions and Slim documentation for additional information.