我需要做什么才能完成这项工作?
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/