最佳答案
我使用 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));
}
我是从..: 厂商/bundle/FOS/UserBundle/Controller/ProfileController.php