Php 细枝中的{exception.message}}不';不呈现HTML
我有一个控制器,在特定条件下引发Php 细枝中的{exception.message}}不';不呈现HTML,php,html,symfony,twig,Php,Html,Symfony,Twig,我有一个控制器,在特定条件下引发\Exception(我还没有弄清楚要使用哪个SF2Exception)。这是: <?php namespace My\AppBundle\Controller; use .... class MyController extends Controller { const EXCEPTION_MESSAGE = <<<EOF My <b>HTML</b> <br/> <small&g
\Exception
(我还没有弄清楚要使用哪个SF2Exception
)。这是:
<?php
namespace My\AppBundle\Controller;
use ....
class MyController extends Controller
{
const EXCEPTION_MESSAGE = <<<EOF
My <b>HTML</b>
<br/>
<small>Small phrase</small>
EOF;
public function indexAction()
{
// my logic
if(in_array($data, $array))
throw new \Exception(self::EXCEPTION_MESSAGE);
// the rest of my logic
return ....
}
}
在代码中捕获异常的地方是需要将异常添加到传递给twig的数组中的地方。比如说
$vars = [];
try {
$a->indexAction();
//fill $vars
} catch (Exception $e) {
$vars['exception'] = $e;
}
//pass $vars to twig
谢谢@exussum回答我的问题,但我没有完全理解。没有一个地方可以捕捉到\Exception
。当满足条件时,将抛出异常,Symfony将显示error.html.twig
视图。在这个视图中,我无法呈现HTML。似乎它会自动呈现。
$vars = [];
try {
$a->indexAction();
//fill $vars
} catch (Exception $e) {
$vars['exception'] = $e;
}
//pass $vars to twig