0
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')
    {


    }
}

Doing nothing inside the controller returns a "Uncaught exception 'Symfony\Component\Routing\Exception\ResourceNotFoundException' in..." error. Not sure if that's right, or if that's another problem. I'd expect it to just do the usual action.

I just need to do it so it shows a specified route exactly as it would if I went to domain.com/page.

I've tried this:

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

...but get this error:

Call to a member function get() on a non-object in...
2
  • Call to a member function get() on a non-object in is a typical case when you are calling a function on a $something that should be an object, but is null instead. Please give us more and complete message, in order to help. Commented Apr 15, 2013 at 6:20
  • Just ignore it then. I just need to know how to access the $controller->forward in an Exception Controller. $this->container->get() obviously doesn't work. Commented Apr 15, 2013 at 6:26

1 Answer 1

3

Your code looks similar to something I did yesterday. I wanted to get all NotFoundHttpException Exception and try to forward them to a default controller. I achieved this with an exception listener like this:

<?php

namespace Acme\MyBundle\Listener;

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;

class NotFoundHttpExceptionListener
{
    protected $container;

    public function setContainer($container)
    {
        $this->container = $container;
    }

    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        $exception = $event->getException();
        if ($exception instanceof NotFoundHttpException) {
            $httpKernel = $this->container->get('http_kernel');

            $response = $httpKernel->forward(
                'AcmeMyBundle:Controller:action',
                array(
                    'uri'  => $_SERVER['REQUEST_URI'],
                )
            );

            $response->headers->set('X-Status-Code', '200');
            $event->setResponse($response);
            $event->stopPropagation();
        }
    }
}

Note that X-Status-Code is necessary if you want to return another status code than 404 because the handleException method in HttpKernel will use this to set the final status code and removes it from the header section.

My services.yml looks something like this:

notfoundhttp.exception.listener:
  class: Acme\MyBundle\Listener\NotFoundHttpExceptionListener
  calls:
    - [ setContainer, [@service_container] ]
  tags:
    - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
Sign up to request clarification or add additional context in comments.

2 Comments

Method 'forward' on http_kernel is deprecated. Any idea?
Ok, as method 'forward is actually deprecated you can make the response this way: $url = $this->container->get('router')->generate('main_homepage'); $response = new \Symfony\Component\HttpFoundation\RedirectResponse($url);

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.