Injecting one service into another results in error: "must be an instance of ..., none given"

StackOverflow https://stackoverflow.com/questions/23518645

سؤال

I created a Listener Service to listen for the Login so that I could perform some database operations after login. I'm trying to inject another service into my Listener but I'm getting a fatal error:

Catchable fatal error: Argument 1 passed to WX\ExchangeBundle\Service\SecurityListener::__construct() must be an instance of WX\ExchangeBundle\Service\UserService, none given

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

I've already tried clearing the cache, but that did not resolve the issue.

هل كانت مفيدة؟

المحلول

There is a typo in your config.yml:

arguements: [@wxexchange_user_service]

must be:

arguments: [@wxexchange_user_service]

(note the 'e' in arguements)

Because of this, the SecurityListener is instantiated without any arguments, but it requires an object of type UserService. That is why you get the error message.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top