2014-10-20 68 views
0

我正在使用身份驗證成功處理程序在每次成功登錄時在會話上填充一些值。我想要完成一些數據庫操作,因此我從我的配置文件中傳遞了@ doctrine.dbal.default_connection。這裏是我的配置文件,我重寫了success_handler函數。在應用程序初始化時獲取doctrine dbal爲空

services:  
    security.authentication.success_handler: 
    class: XYZ\UserBundle\Handler\AuthenticationSuccessHandler 
    arguments: ["@security.http_utils", {}, @doctrine.dbal.default_connection] 
    tags: 
     - { name: 'monolog.logger', channel: 'security' } 

在AuthenticationSuccessHandler.php我的代碼是這樣的......

namespace Sourcen\UserBundle\Handler; 

use Symfony\Component\HttpFoundation\JsonResponse; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; 
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler; 
use Symfony\Component\Security\Http\HttpUtils; 
use Doctrine\DBAL\Connection; 


class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler { 

private $connection; 

public function __construct(HttpUtils $httpUtils, array $options, Connection $dbalConnection) { 
    $this->connection = $dbalConnection; 
    parent::__construct($httpUtils, $options); 
} 

public function onAuthenticationSuccess(Request $request, TokenInterface $token) { 
    $response = parent::onAuthenticationSuccess($request, $token); 
    // DB CODE GOES 
    return $response; 
} 
} 

這是工作,當我直接執行一些控制器URL。但是,當我執行我的應用程序的主頁網址,如「www.xyz.com/web」它拋出以下錯誤......

Catchable fatal error: Argument 3 passed to XYZ\UserBundle\Handler\AuthenticationSuccessHandler::__construct() must be an instance of Doctrine\DBAL\Connection, none given, called in /opt/lampp/xyz/app/cache/prod/appProdProjectContainer.php on line 1006 and defined in /opt/lampp/xyz/src/Sourcen/UserBundle/Handler/AuthenticationSuccessHandler.php on line 18 

任何想法如何解決?

回答

1

您不需要擴展DefaultAuthenticationSuccessHandler類。

嘗試定義您的服務類,如:

namespace XYZ\UserBundle\Handler; 

use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; 
use Doctrine\DBAL\Connection; 


class AuthenticationSuccessHandler { 

private $connection; 

public function __construct(Connection $dbalConnection) { 
    $this->connection = $dbalConnection; 
} 

public function onAuthenticationSuccess(InteractiveLoginEvent $event) { 
    $user = $event->getAuthenticationToken()->getUser(); 

    // DB CODE GOES 
    return $response; 
} 
} 

和配置服務標記到事件偵聽器組件security.interactive_login

services:  
    security.authentication.success_handler: 
    class: XYZ\UserBundle\Handler\AuthenticationSuccessHandler 
    arguments: [@doctrine.dbal.default_connection] 
    tags: 
     - { name: 'kernel.event_listener', event: 'security.interactive_login'. method:'onAuthenticationSuccess' } 

PS:你爲什麼不使用doctrine.orm.entity_manager代替doctrine.dbal.default_connection ? (在我的SF我沒有這個服務傾倒在php app/console container:debug命令)

+0

嗨@約瑟夫,你解決了這個問題? – Matteo 2014-10-22 10:18:14

+0

嗨,matteo,對不起,我不能回覆,因爲我生病了。其實我試圖實現它,但失敗了。所以我把我的構造函數參數從'Connection $ dbalConnection'改爲'Connection $ dbalConnection = null',並且它停止了拋出錯誤。有沒有關於使用db連接使用身份驗證成功處理程序的實用教程?我是symfony的新手,並且無法準確理解爲什麼您給出的上述內容不適用於我當前的設置!非常感謝你的幫助:) – 2014-10-28 02:26:04

+0

嗨@JosephRanjanSRanjanS,你有沒有嘗試我的解決方案?總之:您不需要擴展任何類:只需定義一個事件偵聽器並將其標記到symfony定義的事件即可。如果我找到一些額外的資源(教程等)關於它,我會通知你 – Matteo 2014-10-28 10:45:55