1

I try to override Exception Controller with Symfony 3 I followed their example here http://symfony.com/doc/current/controller/error_pages.html but it doesn't work

So first I have created my error page in app/Ressources/TwigBundle.../error404.html.twig

And it works without the controller overriding.

Second, I have added in services.yml, under services:

frontBundle\Controller\CustomExceptionController:
    public: true
    arguments:
        $debug: '%kernel.debug%'

Third in my Controller folder, I have created a CustomExceptionController.php Inside, I have put (I want to override findtemple() for example)

namespace frontBundle\Controller;

use Symfony\Bundle\TwigBundle\Controller\ExceptionController;
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
use Symfony\Component\HttpFoundation\Request;

class CustomExceptionController extends ExceptionController
{
    /**
     * @param Request $request
     * @param string  $format
     * @param int     $code       An HTTP response status code
     * @param bool    $showException
     *
     * @return TemplateReferenceInterface
     */
    protected function findTemplate(Request $request, $format, $code, 
$showException)
    {

    }
}

However it doesn't work. This new controller is not taken into consideration and the findTemplate() is not overrided.

Did I miss something?

There are really not a lot of help about that with Symfony 3...

Thank you so much

5
  • I precise that I put some code inside protected function findTemplate() which is just a path to a new error404 page name, just to test Commented Oct 31, 2017 at 12:32
  • Did you add the twig exception_controller setting inside your config.yml (as stated in your link)? Commented Oct 31, 2017 at 13:20
  • @ccKep : when I add the "exception_controller: frontBundle:Exception:showException" I have a "Fatal error: Uncaught exception 'Symfony\Component\Routing\Exception\ResourceNotFoundException'... " Commented Nov 1, 2017 at 1:23
  • @ccKep : but do I have to add that line under twig: in my config,yml? Because in Symfony documentation it says: Instead of creating a new exception controller from scratch you can, of course, also extend the default ExceptionController... I am lost, if someone has a clear example it would be great. Thank you. Commented Nov 1, 2017 at 1:24
  • It'd probably be frontBundle:CustomException:show if you extend the default exception controller. (Since your controller is called CustomExceptionController and the inherited method is called showAction) Commented Nov 1, 2017 at 9:39

1 Answer 1

1

If you need override the controller try this:

# app/config/services.yml
services:
    _defaults:
        # ... be sure autowiring is enabled
        autowire: true
    # ...

    AppBundle\Controller\CustomExceptionController:
        public: true
        arguments:
            $debug: '%kernel.debug%'

and:

# app/config/config.yml
twig:
    exception_controller: AppBundle:Exception:showException

Link

Regards

Sign up to request clarification or add additional context in comments.

1 Comment

I added "exception_controller: AppBundle:Exception:showException" and replaced AppBundle by frontBundle and I have an error (see comment above). Do I have to create more file with that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.