简体   繁体   中英

Symfony2 multiple error pages

Most of my Symfony app is separated in two sub-directories for each bundle's controllers "Frontend" and "Backend". What I want to do is have 2 different error pages for frontend and backend 404 errors. Its quite easy to just override the generic twig error template but I was wondering whats the best approach to determine whether it should render the frontend or backend error template. Do I have to rewrite the Twig exception handling for that or there is some built-in functionality do kinda accomplish that. Thanks!

Would backend users only ever use the backend or might they use the frontend too? If authenticated users only use the backend then you could just put an if statement in your 404 template.

{% if is_granted('ROLE_ADMIN') %}
    {# authenticated 404 markup #}
    ...
{% else %}
    {# standard 404 markup #}
    ...
{% endif %}

There's a bundle i've made for that feature, it works from the "kernel.exception_listener" listener and the "kernel.exception" event as described in documentation ( http://symfony.com/doc/current/cookbook/controller/error_pages.html#use-kernel-exception-event ).

With this bundle, you can:

  1. Change the twig exception template from regex of the route you want
  2. Change the twig exception template for certains http status code only
  3. Enable / Disable the custom template for the debug mode

Link to the bundle (MIT licence): https://github.com/Kwrz/TwigException

Hope it's help !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM