使用Symfony2

时间:2018-05-04 07:47:54

标签: symfony service dependency-injection ldap provider

我在使用Symfony2调用ImageEncoderService内的LdapUserProvider.php时遇到问题。在互联网上,我只讨论如何在存储库,控制器,实体和命令中调用服务。

执行此操作:

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

抛出此错误:

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

是否可以将服务注入自定义提供程序?或者我是否需要将其注入其他地方,然后在提供者中使用它?

我使用的LDAP提供商是:https://symfony.com/doc/current/security/ldap.html

PS。该服务本身在应用程序的其他位置工作正常,如命令,控制器等。

1 个答案:

答案 0 :(得分:1)

你确定你有这个API的类使用声明吗?

use  Foo\ApiBundle\Service\ImageEncoderService;

尝试删除缓存目录。

您可以尝试通过services.yml service明确注入。

用于明确注入其他服务和参数的Symfony文档。

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

示例:

     # 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'