Exit Symfony2

My problem is to log out. The code I have is:

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')); } 

The problem is that I can’t access the TokenInterface user TokenInterface when starting the logout function?

+7
source share
3 answers

To get a token, you must enter a security context.

1. Create a class exit listener, something like this:

 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. And then in service.yml, add this line:

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

What is it, can it help.

+11
source

See http://symfony.com/doc/current/reference/configuration/security.html

security.yml

 secured_area: logout: path: /logout **success_handler: logout_listener** 
+6
source

See here if you can overwrite any package controller:

http://symfony.com/doc/current/cookbook/bundles/inheritance.html

0
source

All Articles