简体   繁体   中英

Symfony ERR_TOO_MANY_REDIRECTS

If I call / and am not logged in, I get the error ERR_TOO_MANY_REDIRECTS. Actually, I should be redirected to the login page. Where's the mistake?

security.yaml:

firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    main:
        lazy: true
        provider: app_user_provider
        logout:
            path: app_logout
        form_login:
            login_path: app_login
            check_path: app_login


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

role_hierarchy:
    ROLE_USER:  ROLE_USER
    ROLE_ADMIN: [ROLE_USER, ROLE_ALLOWED_TO_SWITCH]

Controller:

/**
 * @Route("/{id}",defaults={"id" = null} , name="app_dashboard")
 */
public function index(PositiveTimeRepository $positiveTimeRepository, $id): Response
{

    return $this->render('dashboard/index.html.twig', [
        'positiveData' => $positiveTimeRepository->findBy([
            'user' => $this->getUser()]),
        'issetGetID' => $id
    ]);
}

SecurityController:

/**
 * @Route("/login", name="app_login")
 */
public function login(AuthenticationUtils $authenticationUtils): Response
{
    
    $error = $authenticationUtils->getLastAuthenticationError();

    
    $lastUsername = $authenticationUtils->getLastUsername();

    return $this->render('login/login.html.twig', [
        'last_username' => $lastUsername,
        'error' => $error
    ]);
}

I updated my controllers and security.yaml

You don't have to check for the roles in your controller yourself the security package does that with the firewall you defined, but you would have to define your login url:

firewalls:
    main:
        # ...
        form_login:
            # "login" is the name of the route created previously
            login_path: login
            check_path: login

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