我正试图通过自定义身份验证处理程序在数据库中写入一些loggin失败信息。我的问题是访问数据库,因为我不知道条令对象可能存储在的哪里
这是我现在的代码:
namespace MyAppFrontBundleController;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentHttpFoundationRequest as Request;
use SymfonyComponentHttpFoundationRedirectResponse as RedirectResponse;
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SymfonyComponentSecurityHttpAuthentication as Auth;
use SymfonyComponentSecurityCoreExceptionAuthenticationException as AuthException;
class SecurityHandler implements AuthAuthenticationFailureHandlerInterface
{
public function onAuthenticationFailure(Request $request, AuthException $token)
{
try
{
$lastLoginFailure = new DateTime();
// get database object here
}
catch(Exception $ex)
{
}
}
}
有什么想法吗?
将SecurityHandler转换为服务,然后将条令实体管理器注入其中。
http://symfony.com/doc/current/book/service_container.html
- 启动命令php-app/console容器:debug
- 复制
doctrine.orm.entity_manager
并粘贴到您的hadler构造函数参数中,如
[….,@doctory.orm.entity_manager] - 在hadler中使用
DoctrineORMEntityManager;
如果您想使用服务,我认为您应该使用Containerware扩展您的类"SecurityHandler",因为您的安全处理程序不是控制器。
class SecurityHandler extend ContainerAware implements AuthAuthenticationFailureHandlerInterface{
public function onAuthenticationFailure(Request $request, AuthException $token)
{
try
{
$lastLoginFailure = new DateTime();
// get database object here
$doctrine = $this->container->get('doctrine');
$repository = $doctrine->getRepository('*NAME OF REPO*');
}
catch(Exception $ex)
{
}
}
}