doulongti5932 2014-10-20 04:50
浏览 71

在应用程序初始化时获取doctrine dbal是空的

I was using Authentication success handler to populate some values on session on every success login. I wanted some database operation to be done so i pass @doctrine.dbal.default_connection from my config file. Here is my config file where i override success_handler function.

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

In AuthenticationSuccessHandler.php my code is like this...

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

This is working when i execute some controller URL directly. But when i execute my app home url like "www.xyz.com/web" it throws following error...

 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

Any idea how it can be solved ?

  • 写回答

1条回答 默认 最新

  • duanli0119 2014-10-20 07:44
    关注

    You don't need to extends the DefaultAuthenticationSuccessHandlerclass.

    Try to define your service class like:

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

    and configure your service tagged to the event listener component 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: Why don't you use the doctrine.orm.entity_managerinstead of doctrine.dbal.default_connection? (In my sf i haven't this service dumped in the php app/console container:debug command)

    评论

报告相同问题?

悬赏问题

  • ¥15 电视版的优酷可以设置电影连续播放吗?
  • ¥50 复现论文;matlab代码编写
  • ¥30 echarts 3d地图怎么实现一进来页面散点数据和卡片一起轮播
  • ¥15 数字图像的降噪滤波增强
  • ¥15 心碎了,为啥我的神经网络训练的时候第二个批次反向传播会报错呀,第一个批次都没有问题
  • ¥15 MSR2680-XS路由器频繁卡顿问题
  • ¥15 VB6可以成功读取的文件,用C#读不了
  • ¥15 如何使用micpyhon解析Modbus RTU返回指定站号的湿度值,并确保正确?
  • ¥15 C++ 句柄后台鼠标拖动如何实现
  • ¥15 有人会SIRIUS 5.8.0这个软件吗