rafbanaan
rafbanaan

Reputation: 631

Symfony: check if sessions exists in Twig

Is there a way to check if a session exists in the twig file? I want to switch between register or logout in my nav bar based on the session of the user.

I've searched a bit around and I could only found solution that do it in the controller.

Upvotes: 0

Views: 1639

Answers (1)

Jason Roman
Jason Roman

Reputation: 8276

Instead of directly checking session variables, I think you want to use what Symfony already has built-in to handle this. Please see this documentation:

{% if is_granted('IS_AUTHENTICATED_FULLY') %}
    // show logout link
{% else %}
    // show register link
{% endif %}

If you are using a Symfony version earlier than 2.8 you need to check for the existence of app.user first:

{% if app.user and is_granted('IS_AUTHENTICATED_FULLY') %}

Upvotes: 3

Related Questions