0

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.

metad00r
  • 134
  • 1
  • 8

1 Answers1

1

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'
nicandr
  • 341
  • 2
  • 8
  • Thank you, I already did that, but the error keeps appearing. I think it has to do with a dependency injection issue in the Symfony2 LDAP provider. – metad00r May 08 '18 at 07:18
  • Yes, Ldap User provider expects some predefined params in cosntruct, you can see them in https://api.symfony.com/4.0/Symfony/Component/Security/Core/User/LdapUserProvider.html – nicandr May 08 '18 at 15:35
  • Here in the docs you can see what ldap class expects in construct https://symfony.com/doc/current/security/ldap.html – nicandr May 08 '18 at 15:41
  • What can you do, you can inject in your method directly your services, for example: function someStter($bindUsernameBefore = false, $userClass, $em, ImageEncoderService $imgEnconder), and use it in your function, autowiring should be enabled in your system. For em actually you can use dependency injection of Doctrine\Common\Persistence\ManagerRegistry $em and then use it in your method, and then you just need to fetch manager $em->getRepository()->getManager(), or $em->getManager directly, dont remember exactly. – nicandr May 08 '18 at 15:44