Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 3, DI - Add service to argument

Tags:

php

symfony

I have this code

services:
  repo.game:
    class: Doctrine\ORM\EntityRepository
    factory_service: doctrine.orm.default_entity_manager
    factory_method: getRepository
    arguments:
        - AppBundle\Entity\Game

  file.upload.listener:
    class: AppBundle\Listener\FileUploadListener
    arguments: [@repo.game]
    tags:
        - { name: "kernel.event_listener", event: "oneup_uploader.post_upload", method: "onUpload" }

This worked fine in <= 2.8, but here in 3.0 I got this error message

[Symfony\Component\DependencyInjection\Exception\InvalidArgumentException] The file "/ext/thing/app/config/services.yml" does not contain valid YAML.

[Symfony\Component\Yaml\Exception\ParseException] The reserved indicator "@" cannot start a plain scalar; you need to quote the scalar at line 14 (near "arguments: [@repo.game]").

There is nothing else in my /ext/thing/app/config/services.yml file

like image 592
Martin- Avatar asked Dec 03 '15 15:12

Martin-


2 Answers

Referring to the UPGRADE Guide in the yaml section:

Starting an unquoted string with @, `, |, or > leads to a ParseException.

So try to modify your configuration as follow:

 file.upload.listener:
    class: AppBundle\Listener\FileUploadListener
    arguments: ["@repo.game"]
    tags:
        - { name: "kernel.event_listener", event: "oneup_uploader.post_upload", method: "onUpload" }

Hope this help

like image 128
Matteo Avatar answered Nov 12 '22 04:11

Matteo


First error look like the indent, you have 2 indent space and 4 indent in argument and tags, please try test yml online.

Second symfony 3 is strict now. you need add quote in argument service [@repo.game] to ["@repo.game"]

like image 1
hendrathings Avatar answered Nov 12 '22 03:11

hendrathings