I'm using FOSuserbundle to get started with User registration https://github.com/FriendsOfSymfony/FOSUserBundle
我正在使用FOSuserbundle开始用户注册https://github.com/FriendsOfSymfony/FOSUserBundle
I've got it registering / logging in and out. What I want to do now is grab the logged in users data and present it on every page of my site. Like "Hi username" in the header type of thing.
我已经注册/登录了。我现在要做的是获取登录的用户数据并将其呈现在我网站的每个页面上。就像标题类型中的“Hi username”一样。
It seems like embedding a controller in my app/Resources/views/base.html.twig is the best way to do this http://symfony.com/doc/current/book/templating.html#embedding-controllers
似乎在我的app / Resources / views / base.html.twig中嵌入一个控制器是最好的方法来实现这个目的http://symfony.com/doc/current/book/templating.html#embedding-controllers
So I wrote my controller to access the user profile data. What I can't figure out is how to access FOS methods in my embedded controller. So from my Acme/UserBundle/Controller/UserController.php I want to do this:
所以我编写了我的控制器来访问用户配置文件数据。我无法弄清楚如何在我的嵌入式控制器中访问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));
}
which I grabbed from: vendor/bundles/FOS/UserBundle/Controller/ProfileController.php
我从中获取:vendor / bundles / FOS / UserBundle / Controller / ProfileController.php
3 个解决方案
#1
211
You can access user data directly in the twig template without requesting anything in the controller. The user is accessible like that : app.user
.
您可以直接在树枝模板中访问用户数据,而无需在控制器中请求任何内容。用户可以像这样访问:app.user。
Now, you can access every property of the user. For example, you can access the username like that : app.user.username
.
现在,您可以访问用户的每个属性。例如,您可以访问类似的用户名:app.user.username。
Warning, if the user is not logged, the app.user
is null.
警告,如果未记录用户,则app.user为null。
If you want to check if the user is logged, you can use the is_granted
twig function. For example, if you want to check if the user has ROLE_ADMIN
, you just have to do is_granted("ROLE_ADMIN")
.
如果要检查用户是否已记录,可以使用is_granted twig函数。例如,如果要检查用户是否具有ROLE_ADMIN,则只需执行is_granted(“ROLE_ADMIN”)。
So, in every of your pages you can do :
因此,您可以在每个页面中执行以下操作:
{% if is_granted("ROLE") %}
Hi {{ app.user.username }}
{% endif %}
#2
12
For symfony 2.6 and above we can use
对于symfony 2.6及以上版本,我们可以使用
{{ app.user.getFirstname() }}
as app.security global variable for Twig template has been deprecated and will be removed from 3.0
因为已弃用了Twig模板的app.security全局变量,将从3.0中删除
more info:
更多信息:
http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements
http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements
and see the global variables in
并查看全局变量
http://symfony.com/doc/current/reference/twig_reference.html
http://symfony.com/doc/current/reference/twig_reference.html
#3
-1
{{ app.user.username|default('') }}
Just present login username for example, filter function default('') should be nice when user is NOT login by just avoid annoying error message.
只是出现登录用户名,例如,当用户不登录时,过滤器功能默认('')应该是好的,只需避免烦人的错误信息。
#1
211
You can access user data directly in the twig template without requesting anything in the controller. The user is accessible like that : app.user
.
您可以直接在树枝模板中访问用户数据,而无需在控制器中请求任何内容。用户可以像这样访问:app.user。
Now, you can access every property of the user. For example, you can access the username like that : app.user.username
.
现在,您可以访问用户的每个属性。例如,您可以访问类似的用户名:app.user.username。
Warning, if the user is not logged, the app.user
is null.
警告,如果未记录用户,则app.user为null。
If you want to check if the user is logged, you can use the is_granted
twig function. For example, if you want to check if the user has ROLE_ADMIN
, you just have to do is_granted("ROLE_ADMIN")
.
如果要检查用户是否已记录,可以使用is_granted twig函数。例如,如果要检查用户是否具有ROLE_ADMIN,则只需执行is_granted(“ROLE_ADMIN”)。
So, in every of your pages you can do :
因此,您可以在每个页面中执行以下操作:
{% if is_granted("ROLE") %}
Hi {{ app.user.username }}
{% endif %}
#2
12
For symfony 2.6 and above we can use
对于symfony 2.6及以上版本,我们可以使用
{{ app.user.getFirstname() }}
as app.security global variable for Twig template has been deprecated and will be removed from 3.0
因为已弃用了Twig模板的app.security全局变量,将从3.0中删除
more info:
更多信息:
http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements
http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements
and see the global variables in
并查看全局变量
http://symfony.com/doc/current/reference/twig_reference.html
http://symfony.com/doc/current/reference/twig_reference.html
#3
-1
{{ app.user.username|default('') }}
Just present login username for example, filter function default('') should be nice when user is NOT login by just avoid annoying error message.
只是出现登录用户名,例如,当用户不登录时,过滤器功能默认('')应该是好的,只需避免烦人的错误信息。