symfony2 logout symfony2 logout symfony symfony

symfony2 logout


To get token, you must inject with security context.

1. Create class Logout 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]

That's it, may it helps.


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

security.yml

secured_area:    logout:        path:   /logout        **success_handler: logout_listener** 


Take a look here were you can overwrite any controller of the bundle:

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