1
votes

I am using Symfony 3.1. I did follow below url for implementing the custom error page template.

(http://symfony.com/doc/current/controller/error_pages.html#custom-exception-controller).

I creates all files under the same location:

app/
└─ Resources/
   └─ TwigBundle/
      └─ views/
         └─ Exception/
            ├─ error404.html.twig
            ├─ error403.html.twig
            ├─ error.html.twig      # All other HTML errors (including 500)
            ├─ error404.json.twig
            ├─ error403.json.twig
            └─ error.json.twig      # All other JSON errors (including 500)

I did create a bundle with name "TwigBundle" and create controller folder and class with name "TwigBundle\Controller\ExceptionController.php"

Config.yml

Twig Configuration

twig:    
    exception_controller:  TwigBundle:ExceptionController:showException

error.html.twig

    <h1>Page not found</h1>

    {% if is_granted('IS_AUTHENTICATED_FULLY') %}
        {# ... #}
    {% endif %}

    <p>
        The requested page couldn't be located. Checkout for any URL
        misspelling or <a href="{{ path('homepage') }}">return to the homepage</a>.
    </p>

In case you need them, the Ex

i am getting following error message:

Fatal error: Uncaught exception 'Symfony\Component\Routing\Exception\ResourceNotFoundException' in D:\xampp\htdocs\PROJECT_NAME\var\cache\prod\appProdUrlMatcher.php:922 Stack trace: #0 D:\xampp\htdocs\PROJECT_NAME\var\cache\prod\classes.php(1744): appProdUrlMatcher->match('/jkhjjkohuioioi...') #1 D:\xampp\htdocs\PROJECT_NAME\var\cache\prod\classes.php(1613): Symfony\Component\Routing\Matcher\UrlMatcher->matchRequest(Object(Symfony\Component\HttpFoundation\Request)) #2 D:\xampp\htdocs\PROJECT_NAME\var\cache\prod\classes.php(2758): Symfony\Component\Routing\Router->matchRequest(Object(Symfony\Component\HttpFoundation\Request)) #3 [internal function]: Symfony\Component\HttpKernel\EventListener\RouterListener->onKernelRequest(Object(Symfony\Component\HttpKernel\Event\GetResponseEvent), 'kernel.request', Object(Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher)) #4 D:\xampp\htdocs\PROJECT_NAME\vendor\symfony\symfony\src\Symfony\Component\EventDispatcher\Debug\WrappedListener.php(61): call_user_func(Array, Object(Symfony\Component\HttpKernel\ in D:\xampp\htdocs\PROJECT_NAME\var\cache\prod\classes.php on line 3328

I have clear the cache and system is running in Prod ENV. But still i am not getting custom error template.

2
Did you just create the custom templates or did you configure a custom error controller? What specific steps did you perform?Gerry
I just create custom template at above mention location . noting to do for controllerAshish Kumar Saxena
did u added @TwigBundle/Resources/config/routing/errors.xml into your dev routing file? You do not need to rewrite default exception controller.Stephan Yamilov
Okay @Stephan Yamilov can you provide full example ?Ashish Kumar Saxena

2 Answers

1
votes

In first, you need to create new errors templates into this location: /YOUR_PROJECT_FOLDER/app/Resources/TwigBundle/views/Exception/

For sample, let create one: error.html.twig for handle all errors

In second you must write some template content for your errors views. For the experiment, let put this content into our error.html.twig:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Error test. An Error Occurred: {{ status_text }}</title>
</head>
<body>
<h1>WooHoo! Now you can see new error template</h1>
<h2>The server returned a "{{ status_code }} {{ status_text }}".</h2>
</body>
</html>

In third, let check this error in dev environment. To do this put this route into your dev route file /YOUR_PROJECT_FOLDER/app/config/routing_dev.yml (it can be there already)

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

After adding this route, let's check our new error template. go to this link in your web browser:

/app_dev.php/_error/418 where is 418 is just error status code

If you see new error template in develop environment it will be the same on prod env too. Do not forget to clear cache if you want to check it on production environment.

Note that you can create template error404.html.twig for handle only 404 errors. Or you can create error500.xml.twig for xml reponse of 500 error.

0
votes

For me, this is what works flawlessly. Might not be how the docs recommend but its simple to understand. You can try it as an alternative.

\app\Resources\TwigBundle\views\Exception\error.html.twig

{% extends 'layout.html.twig' %}

{% block title %}An Error Occurred: {{ status_text }}{% endblock %}

{% block main %}

    {% if status_code >= 500 %}
        {# twig include 500s server error template here #}
    {% elseif status_code >= 400 %}
        {# twig include 400s user errors template here #}
    {% endif %}

{% endblock %}