Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show customized 404 error page - Symfony2

Tags:

twig

symfony

I have this in my controller:

if(some stuff) {
    throw $this->createNotFoundException('message');
}

When I test it in dev env, I get the Symfony's page telling Exception detected with my text, but I can't test it in prod env :( I run this php app/console cache:clear --env=prod --no-debug and then replace only in the URL app_dev.php with app.php but the toolbar and Symfony's error page stay.

I created this file - app/Resources/TwigBundle/views/Exception/error.html.twig. So will it be rendered in prod?

This is in it:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>An Error Occurred: {{ status_text }}</title>
</head>
<body>
    <h1>Oops! An Error Occurred</h1>
    <h2>The server returned a "{{ status_code }} {{ status_text }}".</h2>
</body>
</html>

where should I give values for status_code and status _text? Or they are taken from the exception?

So in general what I want to do is when a condition in my contoller`s action is true, mine customized error page to be shown in prod env. Did I make it already, and if not, how to make it and how to see the result in prod env?

like image 855
Faery Avatar asked Sep 28 '12 06:09

Faery


People also ask

What is a custom 404 page?

A custom 404 page takes away the confusion of not landing on the page they had intended to land on. It lets your user know that there is an error with their request. Perhaps they mistyped the URL, the page is temporarily unavailable, or the page no longer exists.


2 Answers

you can access your detected text with following way:

{{ exception.message }}
like image 81
meteor Avatar answered Jan 01 '23 07:01

meteor


I also tried and i noticed that custom error pages placed in app/Resources/TwigBundle/views/Exception/error.html.twig worked only in prod environment.

like image 42
Chopchop Avatar answered Jan 01 '23 09:01

Chopchop