gpt4 book ai didi

php - symfony2 注销

转载 作者:可可西里 更新时间:2023-11-01 12:28:39 28 4
gpt4 key购买 nike

我的问题是捕获用户注销。我的代码是:

public  function onAuthenticationFailure(Request $request, AuthenticationException $exception){

return new Response($this->translator->trans($exception->getMessage()));
}

public function logout(Request $request, Response $response, TokenInterface $token)
{
$empleado = $token->getUser();
$log = new Log();
$log->setFechalog(new \DateTime('now'));
$log->setTipo("Out");
$log->setEntidad("");
$log->setEmpleado($empleado);
$this->em->persist($log);
$this->em->flush();
}

public function onLogoutSuccess(Request $request) {
return new RedirectResponse($this->router->generate('login'));
}

问题是当您运行注销功能时,我无法访问用户 token TokenInterface

最佳答案

要获得 token ,您必须注入(inject)安全上下文。

<强>1。创建类注销监听器,如下所示:

namespace Yourproject\Yourbundle\Services;
...
use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;
use Symfony\Component\Security\Core\SecurityContext;

class LogoutListener implements LogoutSuccessHandlerInterface {

private $security;

public function __construct(SecurityContext $security) {
$this->security = $security;
}

public function onLogoutSuccess(Request $request) {
$user = $this->security->getToken()->getUser();

//add code to handle $user here
//...

$response = RedirectResponse($this->router->generate('login'));

return $response;
}
}

<强>2。然后在 service.yml 中,添加这一行:

....
logout_listener:
class: Yourproject\Yourbundle\Services\LogoutListener
arguments: [@security.context]

就是这样,希望对您有所帮助。

关于php - symfony2 注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12743213/

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