简而言之,我试图使用我在自定义处理程序服务中定义的记录器服务。在Symfony2的AuthenticationHandler类中使用自定义记录器服务
我config.yml
services:
authentication_handler:
class: Panasonic\LoginBundle\Handler\AuthenticationHandler
arguments: [ @custom_logger ]
custom_logger:
class: Monolog\Logger
arguments: [login]
calls:
- [ pushHandler, [ @custom_logger_stream ]]
custom_logger_stream:
class: Monolog\Handler\RotatingFileHandler
arguments: [ %kernel.logs_dir%/test.log, 30, 200 ]
“服务:” 在我的代码,DNT的担心以及缩进。
我包/处理器/的AuthenticationHandler
namespace Panasonic\LoginBundle\Handler;
use Monolog\Logger;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class AuthenticationHandler implements AuthenticationSuccessHandlerInterface
{
private $custom_logger;
public function __constructor(Logger $custom_logger)
{
$this->custom_logger = $custom_logger;
}
public function onAuthenticationSuccess(Request $request,
TokenInterface $token)
{
$log = $this->custom_logger;
$log->addWarning('Foo');
$log->addError('Bar');
die;
// var_dump($token); die;
// var_dump($request->getSession()); die;
}
}
我越来越:
Fatal error: Call to a member function addWarning() on a non-object in ... Panasonic\LoginBundle\Handler\AuthenticationHandler.php on line 22
注:我知道我应该返回onAuthenticationSuccess的响应,它其实uncomplete,但我知道它的工作原理,我从var_dumps得到了结果。
我猜喷射不好,应该怎么做?我无法弄清楚在这里要做什么。请帮助
引用我检查: Access services inside a regular class
http://martinsikora.com/symfony2-and-dependency-injection
我觉得这个错误或者在custom_logger:class:Monolog \ LoggerSymfony \ Bridge \ Monolog \ Logger中,并且应该在我的处理程序中使用LoggerInterface。我通过重新编写我的代码来解决我与PéCé给出的链接有关的问题。 – Rishi 2012-04-17 14:03:10