Symfony 4-FOSUserBundle-自定义路由上的模板



Symfony 4应用程序使用FOSUserBundle。

我为用户配置文件(例如配置文件/预订(创建了自定义子路由,并为用户实体(firstName和lastName(添加了一些自定义字段。

如果我在自定义路由(非FOSUserBundle路由(的分支模板中引用{{ user.firstName }},则会出现"未找到用户实体"错误。

如何访问树枝模板中用户的属性?

在渲染视图时,可以使用render()方法的第二个参数中的数组将数据从控制器传递到twitch模板。

这个数组是这样使用的:array('TwigVariableName' => $someValue, 'AnotherTwigVariable' => $someOtherValue)

例如:

//This is a controller
public function bookings()
{
//get the current user
$user = $this->get('security.token_storage')
->getToken()
->getUser();
//Or, simplier, since we are in a controller :
$user = $this->getUser();
return ($this->render('bookings.html.twig', array('user' => $user)));
}

正如Cerad在评论中所说,您可以使用{{ app.user.firstName }}在Twig中直接访问当前用户

最新更新