2012-10-05 15 views
7

मेरी समस्या उपयोगकर्ता लॉगआउट को पकड़ती है। कोड मैं क्या है:symfony2 लॉगआउट

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

समस्या मैं उपयोगकर्ता टोकन TokenInterface उपयोग नहीं कर सकते हैं जब आप लॉगआउट समारोह चल रहे है?

+1

soluction समस्या सेवा सुरक्षा संदर्भ है धन्यवाद। – paradita

+1

'$ टोकन-> getUser()' वापस लौट रहा है? या '$ टोकन 'शून्य है? – Kosta

उत्तर

11

टोकन प्राप्त करने के लिए, आपको सुरक्षा संदर्भ से इंजेक्शन देना होगा। service.yml में

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. और फिर इस लाइन, जोड़ें::

1. वर्ग लॉगआउट श्रोता कुछ इस तरह बनाएँ,

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

यह है कि, हो सकता है यह मदद करता है।

+2

यह लॉगआउट_लिस्टनर सेवा के लिए काम नहीं करेगा, आपके पास कोई परिभाषित ईवेंट नहीं है। तो श्रोता को बिल्कुल नहीं निकाल दिया जाएगा। –

+0

@artworkad シ, मैंने इस कार्यक्रम को service.yml पर पंजीकृत किया है, इसलिए जब उपयोगकर्ता लॉगआउट करता है तो इसे निकाल दिया जाएगा। मैंने इसे कई परियोजना सिम्फनी पर उपयोग किया है, और कोई समस्या नहीं है। – tesmojones

+1

मेरी राय में 24 जुलाई 1414 को 15:22 बजे उत्तर में लाइनों को जोड़ना जरूरी है। यह सिम्फनी 2.5 में मेरे लिए काम किया। – ziiweb