php - symfony: Autowiring 接口(interface)

标签 php symfony autowired

我需要做什么才能完成这项工作?

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

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

Cannot autowire argument 1 for AppBundle\Service\SomeOtherService because the type-hinted class does not exist (Class BaseServiceInterface does not exist).

最佳答案

Autowiring 不直接与接口(interface)一起工作。 您需要创建服务别名才能使其正常工作。

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

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

关于php - symfony: Autowiring 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41395994/

相关文章:

php - 如何在 Osclass 创建多步骤用户注册

java - 在服务器端使用 Java Swing 结合 php 上传文件

symfony 需要完全身份验证才能访问此资源

java - spring 中的动态依赖注入(inject)

spring - 如何在类注解@controller 中获取Spring WebContext

php - php中的文本对齐

php - 查找前 5 条记录的 Eloquent 查询

rest - 在使用 FOSRestBundle 进行序列化期间检测到 Symfony 3.0.4 循环引用

javascript - Symfony 3.3 前端堆栈与 encore - 全局 jquery 插件

java - 无法 Autowiring CRUD 存储库