php - Symfony 3.3 change services to use autowire

679

I want to change my services to use the autowire function of symfony 3. I make it work for most of them, but I still have probem with services using some params.

For services using others services I just delete them from my services file after the autowire config. But I still have services like this

App\MyBundle\Lib\GraphLib: ["@serv.carslib","@serv.buslib","%kernel.root_dir%","@translator","%version%"]

My constructor look like this

public function __construct(CarsLib $carsLib, BusLib $busLib, $rootdir, TranslatorInterface $translator, $version){

Is there a way to autowire this service with parameters? If not how can I change the parameter @serv.carslib and @serv.buslib. Because this services are autowired and can't made somthing like this worked

App\MyBundle\Lib\GraphLib: ["@App\MyBundle\Lib\CarLib","@App\MyBundle\Lib\BusLib","%kernel.root_dir%","@translator","%version%"]

Edit 1 : I try to change my service like this but I have an error 'The service has a dependency on a non-existent service'

App\MyBundle\Lib\GraphLib: 
    arguments:
        $carsLib: "@App\MyBundle\Lib\CarLib"
        $busLib: "@App\MyBundle\Lib\BusLib"
        $rootdir: "%kernel.root_dir%"
        $translator: "@translator"
        $version: "%version%"

I also try somthing like this

App\MyBundle\Lib\GraphLib: 
    arguments:
        $rootdir: "%kernel.root_dir%"
        $version: "%version%"

I changed the params order in my controller like this

public function __construct(CarsLib $carsLib, BusLib $busLib, TranslatorInterface $translator, $rootdir, $version){

But I have this error : Invalid constructor argument 4 for service argument 1 must be defined before

483

Answer

Solution:

You can set manually arguments for your service. Rest of arguments should be autowired . Yourservices.yml should look like this

services:
  #...

  App\Some\Service:
    arguments:
      $arg1: 'arg1'

Here you can read more about manually wiring arguments.

988

Answer

Solution:

I change my service like this with single quote and it works

App\MyBundle\Lib\GraphLib: 
arguments:
    $carsLib: '@App\MyBundle\Lib\CarLib'
    $busLib: '@App\MyBundle\Lib\BusLib'
    $rootdir: '%kernel.root_dir%'
    $translator: '@translator'
    $version: '%version%'

People are also looking for solutions to the problem: PHP Propel cache query on runtime

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.