0

I've got micro-kernel Symfony project with custom catalog structure.

I used this: https://github.com/ikoene/symfony-micro

How can I override e.g. Twig Resources (Exception views)?

Cookbook says that I should create a directory called TwigBundle in my Resources directory.

I made \AppBundle\Resources\TwigBundle\views\Exception directory. Overriding view does not seem to work.

M4ciek
  • 33
  • 9

2 Answers2

1

Thanks for using the microkernel setup. Here's how to override exception views.

1. Create a custom ExceptionController

First off, we're gonna create our own ExceptionController which extends the base ExceptionController. This will allow us to overwrite the template path.

    <?php

    namespace AppBundle\Controller\Exception;

    use Symfony\Bundle\TwigBundle\Controller\ExceptionController as BaseExceptionController;
    use Symfony\Component\HttpFoundation\Request;

    class ExceptionController extends BaseExceptionController
    {
        /**
          * @param Request $request
          * @param string $format
          * @param int $code
          * @param bool $showException
          *
          * @return string
          */
         protected function findTemplate(Request $request, $format, $code, $showException)
         {
             $name = $showException ? 'exception' : 'error';
             if ($showException && 'html' == $format) {
                 $name = 'exception_full';
             }

             // For error pages, try to find a template for the specific HTTP status code and format
             if (!$showException) {
                 $template = sprintf('AppBundle:Exception:%s%s.%s.twig', $name, $code, $format);
                 if ($this->templateExists($template)) {
                     return $template;
                 }
             }

             // try to find a template for the given format
             $template = sprintf('@Twig/Exception/%s.%s.twig', $name, $format);
             if ($this->templateExists($template)) {
                 return $template;
             }

             // default to a generic HTML exception
             $request->setRequestFormat('html');

             return sprintf('@Twig/Exception/%s.html.twig', $showException ? 'exception_full' : $name);
         }
    }

2. Create the error templates

Create templates for the different error codes:

  • error.html.twig
  • error403.html.twig
  • error404.html.twig

In this example, the exception templates would be placed in AppBundle/Resources/views/Exception/

3. Override the default ExceptionController

Now let's point to our new exception controller in the configuration.

twig: exception_controller: app.exception_controller:showAction

Koen
  • 26
  • 2
0

I really like your solution, but I found another way how to do it without custom exception controller.

I realized that automatical additional check for overrided templates happens in directory Resources in the directory when you store your kernel class.

So, for structure in your repo it's:

/Resources/TwigBundle/views/Exception/

Finally I changed a little bit the directory structure to have a 'app' directory with kernel file inside. Just like in the default Symfony project.

M4ciek
  • 33
  • 9