douzuo0002 2017-12-29 13:19
浏览 44
已采纳

Symfony 3.3更改服务以使用autowire

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

  • 写回答

2条回答 默认 最新

  • dongwen4487 2017-12-29 14:16
    关注

    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%'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗