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