Martin-
Martin-

Reputation: 927

Symfony 3, DI - Add service to argument

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

Upvotes: 12

Views: 3764

Answers (3)

Rinat
Rinat

Reputation: 429

For now value with special characters have to be wrapped with single quotes instead of double. It is valid yaml but e.g. it throw many exceptions in phpunit run for symfony

Upvotes: 0

Matteo
Matteo

Reputation: 39460

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

Upvotes: 17

hendrathings
hendrathings

Reputation: 3775

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"]

Upvotes: 1

Related Questions