I'm throwing some exception in my controller.
For example:
throw new AccessDeniedHttpException('some_text');
How can i catch it's 'some_text' parameter in my Twig template?
I found the {{ status_code }} and {{ status_text }} variables, but can't find something similar that solve my problem.
P.S. I'm already use custom error page. I just want give users specific error explanations.
Thnx.
By default Symfony uses the showAction
of Symfony\Bundle\TwigBundle\Controller\ExceptionController
to render your error page. The Implementation in Symfony 2.3 is like:
public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null, $_format = 'html')
{
$currentContent = $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1));
$code = $exception->getStatusCode();
return new Response($this->twig->render(
$this->findTemplate($request, $_format, $code, $this->debug),
array(
'status_code' => $code,
'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '',
'exception' => $exception,
'logger' => $logger,
'currentContent' => $currentContent,
)
));
}
From there you can see that there is 'exception' => $exception
passed to your twig template. $exception
is of type Symfony\Component\HttpKernel\Exception\FlattenException
which is a wrapper for the original PHP Exception.
FlattenException::getMessage
is probably what you want to access your error message. See FlattenException API for more Information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With