Symfony登录表单:TODO:在C:\xampp\htdocs\LicentaTenis\src\Security\AppCustomAuthenticator.php内提供有效的重定向。

3

我是Symfony的新手,这是我的第一个项目,我正在尝试创建一个登录表单。 我使用make:auth创建了它,但是当我尝试登录时,我收到以下错误:

提供有效的重定向内部错误

我替换了

throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);

使用

return new RedirectResponse($this->urlGenerator->generate('home'));

但它仍然显示相同的错误。 这是我的主页路由:
/**
* @Route("/", name="home")
* @Method({"GET"})
*/
public function index(){
    return $this->render('tenis/index.html.twig');
}

你清除缓存了吗? - Michael Sivolobov
我遇到了同样的错误,你是怎么解决的? - Med Karim Garali
3个回答

1

对于第一种解决方案,如果您在用户连接成功时没有任何特定的处理要执行,您可以在 onAuthenticationSuccess(...) 函数中进行空返回(您的AppCustomAthenticator.php文件中的第99行),并直接在 ./config/packages/security.yaml 文件中添加重定向路由,如下所示。

security:
providers:
    # used to reload user from session & other features (e.g.switch_user) #
    app_user_provider:
        entity:
            class: App\Entity\User
            property: email

encoders:
    # use your user class name here
    App\Entity\User:
        # Use native password encoder
        # This value auto-selects the best possible hashing algorithm
        # (i.e. Sodium when available).
        algorithm: auto

firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    main:
        anonymous: lazy
        form_login:
            login_path: app_login
            check_path: app_login
            csrf_token_generator: security.csrf.token_manager # It's optional
            default_target_path: home # add here your redirect path
            always_use_default_target_path: true
        guard:                                        # you need to check this file and 
            authenticators:                           # make sure that all your configurations 
                - App\Security\LoginFormAuthenticator # are right
        logout:
            path: app_logout
            target: app_login

或者您可以在AppCustomAthenticator.php文件中的onAuthenticationSuccess(...)函数中将第99行注释掉。

  throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);

并将其更改为。
return new RedirectResponse($this->urlGenerator->generate('home'));

您可以通过更改重定向Symfony路由的名称来更改“home”的值。

0
在您的SecurityAuthenticator文件中,找到函数SecurityAuthenticator并取消注释以下行: return new RedirectResponse($this->urlGenerator->generate('home')); 以实现重定向。

0
请确保您保存了更改。如果不起作用,请尝试清除缓存: bin/console cache:clear

我使用PhpStorm并且它会自动保存所有更改。我还使用bin/console cache:clear清除了缓存,但是我仍然得到相同的错误。 - Tudor
它仍然显示异常 throw new \Exception('TODO: provide a valid redirect inside '.__FILE__); 并且在第100行吗?这没有意义。 - ping

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接