我正在尝试访问我在 twig 中调用的函数中设置的控制器中的变量。它实际上看起来像这样:
{{ render(controller('AppBundle:Domain:show', {
'test': 'ok'
})) }}
在我的控制器中,它看起来像这样:
public function showAction(Request $request, $test = null)
{
$test = $test ? $test : 'notOk';
var_dump($test); // result "notOK"
die;
}
我的目标是让var_dump($test);
产生"ok"
Twig 中 controller()
的第二个参数是一个请求属性数组。
https://github.com/symfony/twig-bridge/blob/master/Extension/HttpKernelExtension.php#L81
因此:
{{ render(controller('AppBundle:Domain:show', {
'test': 'ok'
})) }}
应该可用:
public function showAction(Request $request)
{
return new Response($request->attributes->get('test'));
}
您必须从控制器返回响应,var_dump不会显示在渲染模板中:
public function showAction(Request $request, $test = null)
{
$test = $test ? $test : 'notOk';
return new Response($test);
}
除此之外,您的示例按预期对我有用