symfony:自动装配接口

Ada*_*ovi 10 php autowired symfony

为了使这项工作,我需要什么?

interface BaseServiceInterface {
   public function getRecords();
} 

class BaseService implements BaseServiceInterface{
    public function getRecords(){
        return "bla";
    }
}


class SomeOtherService{

    private $baseService;

    public function __construct(BaseServiceInterface $baseService){
         $this->baseService = $baseService;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的service.yml看起来像这样:

base_service:
    class: AppBundle\Service\BaseService
    autowire: true
Run Code Online (Sandbox Code Playgroud)

当我尝试运行时,我得到:

无法为AppBundle\Service\SomeOtherService自动装配参数1,因为类型提示类不存在(类BaseServiceInterface不存在).

小智 5

autowire不能直接与接口一起使用。您需要创建服务别名以使其起作用。

services:
    AppBundle\Service\BaseServiceInterface: '@AppBundle\Service\BaseService'
Run Code Online (Sandbox Code Playgroud)

参考:https : //symfony.com/doc/current/service_container/autowiring.html#working-with-interfaces