Generate Static Error Pages using Console Command in Symfony 8

Generate Static Error Pages using Console Command in Symfony 8

Symfony includes a variety of console commands designed to inspect, prepare, and optimize applications. While many commands focus on validation and debugging, others help ensure a smooth user experience even in edge cases. One such case occurs when an HTTP error happens before the Symfony kernel is executed. This tutorial explains how to generate static error pages using console command in Symfony 8 application.

When a web server (such as Nginx or Apache) cannot pass a request to PHP - due to malformed URLs, invalid characters, or early failures - it serves its own default error page instead of Symfony error pages. This behavior can be observed by using an invalid path like https://symfonyapp.local/%.

To guarantee that consistent error pages are shown even in these situations, Symfony provides a way to export error pages as static HTML files.

Symfony offers the error:dump console command, which generates static versions of error pages based on the configured Twig templates. These files can then be used directly by the web server when Symfony itself is not reached.

php bin/console error:dump --env=prod var/cache/prod/error_pages

This command renders all available error pages and stores them as HTML files inside the specified directory.

In some cases, only certain HTTP status codes need to be exported. The error:dump command supports passing one or more status codes as arguments.

php bin/console error:dump --env=prod var/cache/prod/error_pages 404 500

Leave a Comment

Cancel reply

Your email address will not be published.