问题描述
我正在使用身份验证成功处理程序在每次成功登录时填充一些会话值。我想要一些数据库操作完成,所以我从配置文件传递了@ doctrine.dbal.default_connection。这是我的配置文件,我覆盖了success_handler函数。
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' }
在AuthenticationSuccessHandler.php中,我的代码就像这个...
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;
}
}
当我直接执行一些控制器URL时,这工作正常。但是当我执行我的应用程序主页网址如www.xyz.com/web它会抛出以下错误...
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
任何想法如何解决?
推荐答案
您不需要扩展 DefaultAuthenticationSuccessHandler
类。
尝试定义您的服务类,如:
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;
}
}
并将您的服务标记为事件监听器组件 security.interactive_login
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:为什么不使用 doctrine.orm.entity_manager
而不是 doctrine.dbal.default_connection
? (在我的sf中,我没有将此服务转储在 php应用程序/控制台容器中:debug
命令)
PS: Why don't you use the doctrine.orm.entity_manager
instead of doctrine.dbal.default_connection
? (In my sf i haven't this service dumped in the php app/console container:debug
command)
这篇关于在应用程序初始化时,使得doctrine dbal为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!