繁体   English   中英

symfony:自动装配接口

[英]symfony: autowiring an interface

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

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

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

base_service:
    class: AppBundle\Service\BaseService
    autowire: true

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

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

autowire不能直接使用界面。 您需要创建服务别名才能使其正常工作。

services:
    AppBundle\Service\BaseServiceInterface: '@AppBundle\Service\BaseService'

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM