Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony: autowiring an interface

What do I need to in order to make this work?

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

my service.yml looks like this:

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

When I try to run this I get:

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

like image 959
Adam Novakovi Avatar asked Dec 30 '16 12:12

Adam Novakovi


1 Answers

autowire does not work directly with interface. you need to create the service alias to make it works.

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

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

like image 89
Indra Avatar answered Oct 16 '22 14:10

Indra