简体   繁体   中英

symfony route redirecting to login page

I've got a simple controller action:

/**
* @Route("/abc", name="abc")
*/
public function testAction($abc)
{
    return new Response($abc);
}

Security.yml:

security:
    ..

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username

    firewalls:
        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_provider: security.csrf.token_manager # Use form.csrf_provider instead for Symfony <2.4
            logout:       true
            anonymous:    true

    access_control:
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/abc$, role: IS_AUTHENTICATED_ANONYMOUSLY}

But when I go to url /abc it redirects me to login. What am I doing wrong?

First, you have to use placeholder in your route:

/**
 * @Route("/prefix_or_not/{abc}", name="your_route_name")
 */
public function testAction($abc)
{
    return new Response($abc);
}

You should change anonymous: true to anonymous: ~

security:
..

providers:
    fos_userbundle:
        id: fos_user.user_provider.username

firewalls:
    main:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            csrf_provider: security.csrf.token_manager # Use form.csrf_provider instead for Symfony <2.4
        logout:       true
        anonymous:    ~

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM