我正在使用 FOSuserbundle 开始用户注册https://github.com/FriendsOfSymfony/FOSUserBundle
我已经注册/登录和退出。我现在要做的是获取登录的用户数据并将其显示在我网站的每个页面上。就像标题类型中的“嗨用户名”一样。
似乎在我的app/Resources/views/base.html.twig中嵌入控制器是执行此操作的最佳方法http://symfony.com/doc/current/book/templating.html#embedding-controllers
所以我写了我的控制器来访问用户配置文件数据。我想不通的是如何在我的嵌入式控制器中访问 FOS 方法。所以从我的Acme/UserBundle/Controller/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));
}
我从中获取:vendor/bundles/FOS/UserBundle/Controller/ProfileController.php