Reputation: 583
I have a controller's method annotated like this:
/**
* @Route("/document/remove/", name="document_remove", requirements={"id"="^\d+$"}, defaults={"_format"="json"})
* @Method({"DELETE"})
*/
public function removeDocumentAction(Request $request)
If I try to open "/document/remove/" url in my browser (GET request), I see MethodNotAllowedException
. That is right, but I want to get a NotFoundException
instead. How can I do this?
Upvotes: 0
Views: 375
Reputation: 9362
You will need to write an event listener. You are going to subscribe to the kernel exception. Check if the exception is a MethodNotAllowedException and then translate it to a NotFoundException.
//Your Event Listener
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
class ExceptionListener
{
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if (!($exception instanceof MethodNotAllowedException)) {
return;
}
$response = new Response($exception->getMessage(),404);
$event->setResponse($response);
}
}
Have a look at the Symfony Documentation on how to create and register your event listener.
Upvotes: 1