0
votes

I have problems calling my ImageEncoderService inside my LdapUserProvider.php with Symfony2. On the internet, I only find discussions on how to call services inside repositories, controllers, entities, and commands.

Doing this:

class LdapUserProvider implements UserProviderInterface
{
    private $ldapManager;
    private $bindUsernameBefore;
    private $userClass;
    private $em;
    private $session;
    private $imgEncoder;

    public function __construct(LdapManagerUserInterface $ldapManager, $bindUsernameBefore = false, $userClass, $em, ImageEncoderService $imgEnconder)
    {
        $this->session = new Session();
        $this->ldapManager = $ldapManager;
        $this->bindUsernameBefore = $bindUsernameBefore;
        $this->userClass = $userClass;
        $this->em = $em;
        $this->imgEnconder = $imgEnconder;
    }
    ...
}

Throw this error:

ContextErrorException in LdapUserProvider.php line 58:
Catchable Fatal Error: Argument 5 passed to
Foo\ApiBundle\Provider\LdapUserProvider::__construct() 
must be an instance of Foo\ApiBundle\Service\ImageEncoderService,
none given, called in /app/cache/dev/appDevDebugProjectContainer.php 
on line 2202 and defined

Is it even possible to inject the service into a custom provider? Or do I need to inject it somewhere else and then use it inside the provider?

The LDAP provider I'm using is this: https://symfony.com/doc/current/security/ldap.html

PS. The service itself works fine in other places of the app, like commands, controllers etc.

1

1 Answers

1
votes

are you sure that you have in class use statement of this API?

use  Foo\ApiBundle\Service\ImageEncoderService;

Try to delete cache directory.

You can try inject explicitly through services.yml service.

Symfony documentation for injecting explicitly other services and params.

https://symfony.com/doc/current/service_container.html#manually-wiring-arguments

Example:

     # explicitly configure the service
        #dont remember if you need to register the service like this
        #but should work without this, refer documentation :)
        # ImageService:
        #   class:  Foo\ApiBundle\Provider\LdapUserProvider

        Foo\ApiBundle\Provider\LdapUserProvider:
            arguments:
                $imgEncoder: '@Foo\ApiBundle\Service\ImageEncoderService'
                #$imgEncoder: '@ImageService'