AskHandle Blog
How to Customize Error Pages in Symfony Framework

How to Customize Error Pages in Symfony Framework
Are you looking to enhance the default error pages in your Symfony web application? Personalizing these pages can improve the user experience when issues arise. This article outlines the process for customizing error pages in Symfony.
Understanding Symfony's Default Error Pages
Symfony generates standard error pages when an error occurs in an application. These pages provide essential information, such as the error type and a stack trace. While helpful to developers, they often do not meet the needs of end-users looking for clear guidance.
Why Customize Error Pages?
Custom error pages serve multiple purposes:
- They maintain the visual consistency of your application, matching error messages with your branding.
- They improve user experience by offering clear instructions and support options when errors occur.
How to Customize Error Pages in Symfony
Step 1: Create Custom Error Templates
To begin customizing error pages, create custom error templates. Navigate to the templates directory in your Symfony project and create a folder named errors if it does not exist.
Inside the errors folder, add individual Twig templates for each error type. For example, create:
error404.html.twigfor 404 errors (page not found)error500.html.twigfor 500 errors (server errors)
Here is an example of a custom 404 error template (error404.html.twig):
1{# templates/errors/error404.html.twig #}
2
3{% extends 'base.html.twig' %}
4
5{% block title %}Page Not Found{% endblock %}
6
7{% block body %}
8 <h1>Oops! Page Not Found</h1>
9 <p>The page you are looking for could not be found.</p>
10 <p><a href="{{ path('home') }}">Return to Home</a></p>
11{% endblock %}Step 2: Configure Custom Error Pages
After creating the templates, configure Symfony to use them. Open the config/packages/twig.yaml file and add the following:
1# config/packages/twig.yaml
2
3twig:
4 exception_controller: App\Controller\ErrorController::showErrorNext, create the ErrorController in your project with a method named showError:
1// src/Controller/ErrorController.php
2
3namespace App\Controller;
4
5use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
6use Symfony\Component\HttpFoundation\Response;
7
8class ErrorController extends AbstractController
9{
10 public function showError(\Throwable $exception): Response
11 {
12 $statusCode = $exception->getStatusCode();
13
14 return $this->render("errors/error{$statusCode}.html.twig", [], new Response('', $statusCode));
15 }
16}This controller retrieves the error status code and renders the appropriate custom error template.
Step 3: Test Your Custom Error Pages
To test the custom error pages, trigger an error by navigating to a non-existent route or throwing an exception. You should see your custom error page displaying, improving the experience for your users.
Customizing error pages in Symfony enhances the user experience of your web application. By creating custom templates and configuring your application, you align error messages with your branding, providing clear and actionable information when issues arise.
Start customizing your error pages to create a more engaging experience for your users.