Search code examples
phpsymfonysymfony-2.3symfony-security

Can't get the logout action sonata-project


This is my security.yml file

firewalls:
    main:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            csrf_provider: form.csrf_provider
            login_path: project_frontend_main_index
        logout: 
            path: project_frontend_main_logout
            anonymous: true

access_control: 
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY } 
    - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY } 
    - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY } 
    - { path: ^/admin/, role: ROLE_ADMIN } 
    - { path: ^/alerts*, role: ROLE_USER }

My question is The problem is with logout, I can't access logout function in the Main controller. I get this error when i click "logout" You must activate the logout in your security firewall configuration.


Solution

  • Try to add this on the top of your access_control :

    - { path: ^/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY } 
    

    Also, you have to add the target for the logout (where the user will be redirected :

    logout: 
        path: project_frontend_main_logout
        target: / #or a specific public route
    

    If none of this works, use the default configuration for a sonata project that can be found here :

    security:
        providers:
            fos_userbundle:
                id: fos_user.user_manager
    
        firewalls:
            main:
                pattern:      .*
                form-login:
                    provider:       fos_userbundle
                    login_path:     /login
                    use_forward:    false
                    check_path:     /login_check
                    failure_path:   null
                logout:       true
                anonymous:    true