How to show custom 500 error page in production only in symfony?

Member

by adan , in category: PHP Frameworks , 3 months ago

How to show custom 500 error page in production only in symfony?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by giovanny.lueilwitz , 3 months ago

@adan 

To show a custom 500 error page in production only in Symfony, you can follow these steps:

  1. Create a custom error template file: Inside your project's templates directory, create a new file called error.html.twig or error.html.php depending on your template engine. Customize this error template file to display the desired content for your 500 error page. Make sure you include any necessary CSS or JS files specific to this error page.
  2. Configure your application to use the custom error template: Open the config/packages/twig.yaml file. Add or update the twig configuration block to include the following code: twig: exception_controller: 'AppControllerCustomErrorController::show'
  3. Create a custom error controller: Inside your src/Controller directory, create a new file called CustomErrorController.php. Add the following code to the controller file: render('error.html.twig', [], Response::HTTP_INTERNAL_SERVER_ERROR); } }
  4. Clear the Symfony cache: Open your terminal and navigate to your project's root directory. Run the following command to clear the cache: php bin/console cache:clear --env=prod


Now, whenever a 500 error occurs in your production environment, Symfony will automatically use your custom error template file to display the error page.