我使用FOSuserbundle上手的用户注册https://github.com/FriendsOfSymfony/FOSUserBundle访问登录的用户在模板
我知道了注册/登录和注销。我现在要做的是抓取登录的用户数据并将其显示在我网站的每个页面上。像“Hi用户名”在标题类型的东西。
好像在我应用程序/资源/视图/ base.html.twig嵌入控制器做到这一点http://symfony.com/doc/current/book/templating.html#embedding-controllers
所以我写了我的控制器来访问用户配置文件数据的最佳方式。我想不出的是如何在我的嵌入式控制器中访问FOS方法。所以从我的阿克米/ UserBundle /控制器/ UserController.php我想这样做:
public function showAction()
{
$user = $this->container->get('security.context')->getToken()->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException(
'This user does not have access to this section.');
}
return $this->container->get('templating')
->renderResponse('FOSUserBundle:Profile:show.html.'.$this->container
->getParameter('fos_user.template.engine'), array('user' => $user));
}
这是我从抓起: 供应商/包/ FOS/UserBundle /控制器/ ProfileController.php
谢谢。我实际上已将“ROLE”更改为“IS_AUTHENTICATED_REMEMBERED”,并且效果很好。 – ed209
我没有使用FOSuserbundle(除非它现在带有),但'app.user.username'仍然适用于我。可能对有人知道有用。 –
谢谢!在文档中这应该更清晰。 –