How to show customized 404 error page - Symfony2
Asked Answered
C

5

12

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?

Cadena answered 28/9, 2012 at 6:49 Comment(0)
H
16

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.

Hognut answered 1/10, 2012 at 14:34 Comment(3)
For anyone googling their way here: Try forcing $useDebugging = false; in index.php. You'll need to clear caches afterwards.Senseless
For me this does not work. And I do not know why. I even cleared the cache. I use version 3.1.3Tempo
If I put a file named exception_full.html.twig in the directory works fine. But. all the errors templates will be overridden by it. But, I need different templates for each error code... sfy docs says I would have to place a file named errorxxx.html.twig ( xx is the error code ). But, it does not work like this. Any Idea ??? ( symfony.com/doc/current/controller/error_pages.html )Guillemette
K
14

Better late than never I guess..

You probably know about overriding of templates. You can override any template you want. So if you want to override the dev error page, just for debugging, then you should override the template:

exception_full.html.twig

You can do so by creating such a file in this folder:

app/Resources/TwigBundle/views/Exception

Now you will see your customized 404 in dev mode.

Kaz answered 31/5, 2013 at 12:37 Comment(2)
I had to clear the cache (SF2.1) to be able to see the overridden template. Best workarround found yet.Burgundy
If I put a file named exception_full.html.twig in the directory works fine. But. all the errors templates will be overridden by it. But, I need different templates for each error code... sfy docs says I would have to place a file named errorxxx.html.twig ( xx is the error code ). But, it does not work like this. Any Idea ??? ( symfony.com/doc/current/controller/error_pages.html )Guillemette
C
9

you can access your detected text with following way:

{{ exception.message }}
Confirmation answered 23/11, 2014 at 18:3 Comment(0)
A
5

Read the doc http://symfony.com/doc/2.0/cookbook/controller/error_pages.html

and Customizing the 404 Page and other Error Pages

http://symfony.com/doc/2.0/cookbook/controller/error_pages.html#customizing-the-404-page-and-other-error-pages

Amylolysis answered 28/9, 2012 at 6:52 Comment(4)
I've read them and done what they say, but I'm stuck with the dev env and I don't know if it's working and how to see the result.Cadena
Try it in prod env, the errors are shown in dev (to help you) so it'll not work.Zobkiw
You answer would have been better if the element of answer were also on StackOverflow in not in an external link that might change or move.Flowerage
This does not answer to the question. The OP is asking for viewing prod error templates in dev.Burgundy
W
3

It appears Symfony has a new way of allowing you to see the error page templates in your dev environment. Taken from their docs:

While you're in the development environment, Symfony shows the big exception page instead of your shiny new customized error page. So, how can you see what it looks like and debug it?

Fortunately, the default ExceptionController allows you to preview your error pages during development.

To use this feature, you need to have a definition in your routing_dev.yml file like so:

# app/config/routing_dev.yml
_errors:
    resource: "@TwigBundle/Resources/config/routing/errors.xml"
    prefix:   /_error

Read more: Symfony Error Pages Manual

Whortleberry answered 10/5, 2016 at 17:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.