将一个服务注入另一个服务会导致错误:"必须是...的实例,没有给出"

时间:2014-05-07 13:03:37

标签: php symfony dependency-injection

我创建了一个监听器服务来监听登录,以便我可以在登录后执行一些数据库操作。我正在尝试向我的Listener中注入另一项服务但是我遇到了致命的错误:

  

可捕获的致命错误:参数1传递给   WX \ ExchangeBundle \ Service \ SecurityListener :: __ construct()必须是   WX \ ExchangeBundle \ Service \ UserService的实例,没有给出

config.yml:

services:
    wxexchange_user_service:
        class:      WX\ExchangeBundle\Service\UserService
        arguments:  [@doctrine.orm.default_entity_manager]
    wxexchange_login_listener:
        class:      WX\ExchangeBundle\Service\SecurityListener
        arguements: [@wxexchange_user_service]
        tags:
            - { name: kernel.event_listener, event: security.interactive_login, method: onSecurityInteractiveLogin }

SecurityListener:

namespace WX\ExchangeBundle\Service;

use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use WX\ExchangeBundle\Service\UserService;

class SecurityListener
{
    protected $userService;

    public function __construct(UserService $userService)
    {
        $this->userService = $userService;
    }

    public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
    {
        $lastLogin = new DateTime();

        $user = $event->getAuthenticationToken()->getUser();

        $this->userService->setLastLogin($user, $lastLogin);
    }
}

我已经尝试清除缓存,但这并没有解决问题。

1 个答案:

答案 0 :(得分:2)

config.yml中有一个拼写错误:

arguements: [@wxexchange_user_service]

必须是:

arguments: [@wxexchange_user_service]

(注意' e' in arguements)

因此,SecurityListener实例化时没有任何参数,但它需要类型为UserService的对象。这就是您收到错误消息的原因。