相关疑难解决方法(0)

访问模板中的登录用户

我正在使用FOSuserbundle开始用户注册https://github.com/FriendsOfSymfony/FOSUserBundle

我已经注册/登录和注销了.我现在要做的是获取登录用户数据并将其呈现在我网站的每个页面上.像标题类型的东西中的"Hi username".

似乎在我的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));
}
Run Code Online (Sandbox Code Playgroud)

我从中获取:vendor/bundles/FOS/UserBundle/Controller/ProfileController.php

symfony twig fosuserbundle

92
推荐指数
2
解决办法
10万
查看次数

标签 统计

fosuserbundle ×1

symfony ×1

twig ×1