我在控制器中抛出了一些异常。
例如:
throw new AccessDeniedHttpException('some_text');
如何在Twig模板中捕获'some_text'参数。
我找到了{{status_code}}和{{status_text}}变量,但无法找到解决我问题的类似内容。
P.S。我已经使用自定义错误页面了。我只想给用户特定的错误解释。
日Thnx。
答案 0 :(得分:4)
默认情况下,Symfony使用showAction
Symfony\Bundle\TwigBundle\Controller\ExceptionController
来呈现您的错误页面。 Symfony 2.3中的实现类似于:
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,
)
));
}
从那里你可以看到有'exception' => $exception
传递给你的树枝模板。 $exception
的类型为Symfony\Component\HttpKernel\Exception\FlattenException
,它是原始PHP异常的包装器。
FlattenException::getMessage
可能就是您要访问错误消息的内容。有关详细信息,请参阅FlattenException API。
答案 1 :(得分:3)
确定。 TWIG代码是
{{ exception.message|nl2br }}