2013-09-30 67 views
1

我對認證成功如下處理:onAuthenticationSuccess給我的事件作爲參數

app_auth_success_handler: 
     class: App\UserBundle\Security\User\Handler\LoginAuthSuccessHandler 
     public: true 
     arguments: ['@router', "@service_container"] 
     tags: 
      - { name: kernel.event_listener, event: security.authentication.success, method: onAuthenticationSuccess } 

class LoginAuthSuccessHandler implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface 
{ 
    private $router; 
    private $container; 

    /** 
    * Constructor 
    * @param RouterInterface $router 
    */ 
    public function __construct(RouterInterface $router, $container) 
    { 
     $this->router = $router; 
     $this->container = $container; 
    } 

    public function onAuthenticationSuccess(Request $request, TokenInterface $token) 
    { 

但是之後我驗證用戶它給了我這個錯誤:

Catchable fatal error: Argument 1 passed to App\UserBundle\Security\User\Handler\LoginAuthSuccessHandler::onAuthenticationSuccess() must be an instance of Symfony\Component\HttpFoundation\Request, instance of Symfony\Component\Security\Core\Event\AuthenticationEvent given in /Users/John/Sites/App/src/Shopious/UserBundle/Security/User/Handler/LoginAuthSuccessHandler.php on line <i>31</i></th></tr> 

想不通這是爲什麼..

回答

2

您錯誤地將security.authentication.success事件與您可以在防火牆配置中定義的success_handler相混淆。

success_handler的服務可以是執行AuthenticationSuccessHandlerInterface的任何服務。

use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface; 
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; 

class LoginAuthSuccessHandler implements AuthenticationSuccessHandlerInterface 
{ 
    public function onAuthenticationSuccess(Request $request, TokenInterface $token) {} 
} 

它並不需要標記爲一個事件偵聽器:

app_auth_success_handler: 
    class: App\UserBundle\Security\User\Handler\LoginAuthSuccessHandler 
    public: true 
    arguments: ['@router', "@service_container"] 

不是服務名稱傳遞到防火牆配置:

security: 
    firewalls: 
     main: 
      form_login: 
       success_handler: app_auth_success_handler 
相關問題