gpt4 book ai didi

symfony - 尝试使用 targetPath 登录 Symfony 后重定向用户,但它始终为 null

转载 作者:行者123 更新时间:2023-12-02 20:15:37 27 4
gpt4 key购买 nike

我已经看到了$this->getTargetPath($request->getSession(), $providerKey)可以用来获取目标路径,但它总是 null .

有人可以解释一下它的用途以及为什么它总是 null对我来说?

use Symfony\Component\Security\Http\Util\TargetPathTrait;


class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
{

use TargetPathTrait;

public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
$targetPath = $this->getTargetPath($request->getSession(), $providerKey);

if (!$targetPath) {
$targetPath = $this->container->get('router')
->generate('poll_index');
}

return new RedirectResponse($targetPath);
}
}

最佳答案

从 Symfony 设置目标路径的唯一时间是当用户启动身份验证流程并通过身份验证入口点时。这是由 ExceptionListener 完成的。如果您使用的是 FormAuthenticationEntryPoint,当您尝试访问受限页面时就会发生这种情况,并且您(通常)会被重定向到登录页面。此时目标路径已设置。

通常没有理由自己设置它,但您可以通过使用 TargetPathTrait 的 saveTargetPath 来实现,如下所示:

$this->saveTargetPath($request->getSession(), $providerKey, $request->getUri());

关于symfony - 尝试使用 targetPath 登录 Symfony 后重定向用户,但它始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52464935/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com