Yes! If I understand your use-case correctly, you can create a custom exception controller:
- Redefine the
twig.controller.exception.class parameter
- Create a custom controller
Here's an example from one of my projects:
In app/config/services.yml or wherever is appropriate for your project, add:
parameters:
twig.controller.exception.class: AppBundle\Controller\ExceptionController
Then create custom controller src/AppBundle/Controller/ExceptionController.php:
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\TwigBundle\Controller\ExceptionController as BaseExceptionController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\FlattenException;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
/**
* ExceptionController
*
* Override standard exceptions
* in production env with basic
* json response codes.
*
* @see app/config/services.yml
*/
class ExceptionController extends BaseExceptionController
{
/**
* {@inheritdoc}
*/
public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null)
{
if ($request->attributes->get('showException', $this->debug)) {
return parent::showAction($request, $exception, $logger);
}
return new JsonResponse(null, $exception->getStatusCode());
}
}
In my project this returns {} with the appropriate HTTP response code in prod. Obviously your use-case might require different logic, which you can implement easily enough in your custom ExceptionController::showAction() method.
Hope this helps :)