2

在 Symfony 5.0 应用程序中,我有以下场景:

管理员用户能够创建新用户。如果以这种方式创建了一个新用户,我想注销管理员重定向到登录屏幕并将电子邮件字段中的预填充值设置为之前创建的用户之一。

目前,我与 SecurityController 有一个链接,href="{{ path('app_logout', {email: user.email}) }}" 我得到了像这样定义的默认注销方法

/**
 * @Route("/logout", name="app_logout")
 */
public function logout()
{
    throw new \Exception('This method can be blank - it will be intercepted by the logout key on your firewall');
}

那么......我将如何处理“电子邮件”参数并将其传递给登录功能以在那里处理它?

4

1 回答 1

1

使用依赖注入获取Symfony\Component\HttpFoundation\Request对象,然后email从那里获取参数:

/**
 * @Route("/logout", name="app_logout")
 */
public function logout(Request $request)
{
    if ($request->get('email') {
        return $this->redirectToRoute('security_login', [
            'email' => $request->get('email');
        ]);
    }
}

/**
 * @Route("/login", name="security_login")
 */
public function login(Request $request, AuthenticationUtils $authenticationUtils, TokenStorageInterface $tokenStorage)
    {
        // force logout of previous user
        $tokenStorage->setToken(null);

        // get the login error if there is one
        $error = $authenticationUtils->getLastAuthenticationError();

        $form = $this->createForm(LoginForm::class, [
            'email' => $request->get('email');
        ]);

        return $this->render('security/login.html.twig', [
            'form' => $form->createView(),
            'error' => $error,
        ]);
}
于 2020-02-02T16:36:41.763 回答