Domanda

I'm using Symfony2. I have a main twig template with a navbar where I want to have a pull-down menu with the options 'log in' or 'log out' depending on whether the user is logged in or not. What is the best approach to accomplish this?

-By making a different, static main template for /admin/* with the log out option

-By checking whether the user is logged in or not inside each controller being called by the routing system and then passing the correct information to be shown to the twig template when it's time to render it

-By calling a specific controller like buildNavbarAuthOption() from inside the template

Last option doesn't seem the best one when trying to program using MVC, right?

È stato utile?

Soluzione

Just use is_granted in your views:

{% if is_granted('IS_AUTHENTICATED_FULLY') %}
    <a href="{{ path('name_of_logout_path') }}">Logout</a>
{% else %}
    <a href="{{ path('name_of_login_path') }}">Login</a>
{% endif %}

Altri suggerimenti

In your main template you could test if the variable app.user exists like this:

{% if app.user %}
    {# display logout #}
{% else %}
    {# display login #}
{% endif %}

Or better yet, test if the current visitor has the minimum security role:

{% if is_granted('IS_AUTHENTICATED_REMEMBERED') %}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top