0

I have looked at so many ways to do this but can't find a solution.

I want my site to show a specific route/page when an exception occurs.

So, if I set it to show the route 'contact' and I go to domain.com/dfergerxyttt (no route found) then it will be the same as if I had gone to domain.com/route (except for a 404 status code).

I need these key things though: (1) I need it to run the controller code as it normally would (2) I need it to also run my Request event listener code

I can't just set it to show one specific template, it must be the whole request-event-listener and controller etc. process.

I tried an exception listener but it wouldn't run the request listener as well. All that code was ignored.

I can't see a way to just set a catch-all route or anything either. To set a default or a catch-all route would be a good idea.

--

I've tried this, but I can't get access to the controller. Not sure if I need to inject it in or something:

namespace etc...

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\TwigBundle\Controller\ExceptionController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\Exception\FlattenException;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
use Symfony\Component\HttpFoundation\Response;

class MyExceptionController extends ExceptionController
{
    public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null, $format = 'html')
    {

       $httpKernel = $this->container->get('kernel');
       $response = $httpKernel->forward('AcmeMyBundle:Default:pageAction');
       $this->setResponse(new Response($response));

    }
}

I've also done this in services.yml which does seem to be correct:

parameters:
    twig.controller.exception
      class: Acme\MyBundle\MyExceptionController
user2143356
  • 5,467
  • 19
  • 51
  • 95

1 Answers1

0

Create an Exception Controller and use Controller::forward to forward to a specific route.

See this answer on how to create an exception controller in 2.2: https://stackoverflow.com/a/15773923/1149495

Community
  • 1
  • 1
Wouter J
  • 41,455
  • 15
  • 107
  • 112
  • I've done all this, but I can't access Controller. Just Controller::forward(--) doesn't work and neither does $this->controller(--). I just need access to the controller. – user2143356 Apr 15 '13 at 06:29
  • please show us what you've done. It seems like you don't understand something – Wouter J Apr 15 '13 at 06:55
  • I've updated the question with what I've tried. I think it's just getting access to the controller class/object that I need. – user2143356 Apr 15 '13 at 08:06
  • Thanks for the help btw. I notice you have the Symfony2 badge so clearly know what your're talking about, but I definitely can't use $this->forward like a normal controller. – user2143356 Apr 15 '13 at 09:01
  • @user2143356 do you use Symfony2.2+ or something lower than 2.2? – Wouter J Apr 15 '13 at 09:04
  • @user2143356 since 2.2 the ExceptionController is a service, that means you can't access the helper methods. I'll write how to do it with Sf2.2 in some hours. – Wouter J Apr 15 '13 at 13:49