CoderTn
CoderTn

Reputation: 997

get the current user id in twig symfony 3.4

i am using FosUserBundle in Symfony 3.4 i want to get the current user id and pass it in URL to another page from twig , this is my code :

<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>

but the problem is i get the url like this

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

the id is '%202' while it's supposed to be an integer '2'

Upvotes: 0

Views: 8605

Answers (1)

balzacLeGeek
balzacLeGeek

Reputation: 815

When you have an User in your app session, you can fetch all your User information from Twig with this: {{ app.user }} Then, to get the User Id, use {{ app.user.id }}

Some remarks, if you really want to set the url manually, This is the best practice:

{% 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>

Upvotes: 3

Related Questions