在twig symfony 3.4中获取当前用户ID

问题描述 投票:0回答:1

我在Symfony 3.4中使用FosUserBundle我想获取当前用户ID并将其从URL传递到另一个页面,这是我的代码:

<a href="{{ absolute_url(asset('')) }}app_dev.php/ajout/{{ event.id }}/{% if is_granted("IS_AUTHENTICATED_REMEMBERED") %} {{  app.user.id|number_format  }}
{% endif %}">Réserver</a>

但问题是我得到这样的网址

http://localhost/techeventsweb/web/app_dev.php/ajout/1/%202

id是'%202',而它应该是一个整数'2'

symfony fosuserbundle
1个回答
0
投票

当您在应用会话中拥有用户时,您可以使用以下命令从Twig获取所有用户信息:{{ app.user }}然后,要获取用户ID,请使用{{ app.user.id }}

一些评论,如果你真的想手动设置网址,这是最好的做法:

{% if is_granted("IS_AUTHENTICATED_REMEMBERED") %}
    {% set url = absolute_url(asset('')) ~ 'app_dev.php/ajout/' ~ event.id ~ '/' ~ app.user.id %}
{% else %}
    {% set url = "OTHER_URL" %}
{% endif %}

<a href="{{ url }}">Réserver</a>
© www.soinside.com 2019 - 2024. All rights reserved.