Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2: Create a logout link

I am managing users in my symfony2 project. I am able to login and logout without problems. Now, I would like to create a logout link (instead of typing app_dev.php/logout).

I have tryed

<a href="/logout">Logout</a>

But this link is deleting the app_dev.php and i have only /logout which does not exist.

here is my security.yml

security:
  encoders:
    MDPI\BackendBundle\Entity\Users:
      id: mdpi.backend.backendencoder.class

  providers:
    secured_area:
     entity: { class: MDPI\BackendBundle\Entity\Users, property: email }

  firewalls:
    secured_area:
      pattern:    ^/
      anonymous: ~
      form_login:
        login_path:  /login
        check_path:  /login_check
      logout:
         path:   /logout
         target: /


  access_control:
    - { path: ^/(?!login)(.*), role: ROLE_admins }
    - { path: /login.*, role: IS_AUTHENTICATED_ANONYMOUSLY }

Thank you very much.

like image 514
Milos Cuculovic Avatar asked Sep 26 '12 09:09

Milos Cuculovic


Video Answer


3 Answers

I created the link this way: (I use TWIG)

<a href="{{ path('user_logout') }}">Logout</a>

And in my "UserBundle", in the routing.yml file added

user_logout:
       pattern:   /logout
like image 65
Bernat Avatar answered Nov 06 '22 02:11

Bernat


When you are using the FOSUserBundle you can use

{{ path('fos_user_security_logout') }}
like image 36
Matthias Kleine Avatar answered Nov 06 '22 02:11

Matthias Kleine


Since Symfony 2.7 you can use Twig logout_path or logout_url functions.

like image 33
MatTheCat Avatar answered Nov 06 '22 04:11

MatTheCat