Search code examples
symfonytemplatesexceptiontwigbundle

Use bundle error templates in Symfony 3 app


I have a MyBundle bundle which I use in many Symfony applications. This bundle provides common things which are shared across these applications, such as Controllers, Entities, templates, etc.

I would like the bundle to provide error templates as well. I tried to put the templates in MyBundle/Resources/TwigBundle/views/Exception folder but the application does not use them.

When I copy TwigBundle folder from MyBundle/Resources into app/Resources then the templates are used as expected. However I do not want to copy the templates into every application's app/Resources folder.

Is it possible to override error templates using MyBundle/Resources instead of app/Resources?


Solution

  • TwigBundle always by default checks directory app/Resources/TwigBundle/views/Exception/ for templates.

    If you want to use custom directory you have to override ExceptionController. You can do this in config

    // app/config/config.yml
    twig:
        exception_controller: MyBundle:Exception:showException
    

    Default Twig ExceptionController can be found here.

    Documentation