Symfony2 ScopeCrossingInjectionException when you use prototype scope

scope, service, symfony

Solution

I guess symfony's synchronized services may help you: doc

otherwise you can set "strict=false" in this way:

services:
    my.service.definition:
        class: Acme\Services\BlaService
        arguments:
            - "@any_other_service_from_narrower_scope="
        scope: prototype

The "=" at the end of the service-definition when injecting a service from a narrower scope, will turn "strict" to false

Problem

My aim is to add data_collector to my classes for displaying some useful information on the developer toolbar. My service: ``` services: my_api.auth.login: class: YO\ApiV1\Services\Auth\Login arguments: - requestId - "@old_sound_rabbit_mq.login_rpc" - "@service_container" scope: prototype ``` I need scope prototype to have different instance for every new call. By the way, service @old_sound_rabbit_mq.login_rpc has scope "prototype" as well. And, I'd like to attach data_collector, which could be done with: ``` tags: - { name: data_collector, template: "AcmeDebug:Collector:templatename", id: "your_collector_name" } ``` But then I got an exception: ScopeCrossingInjectionException: Scope Crossing Injection detected: The definition "profiler" references the service "my_api.auth.login" which belongs to another scope hierarchy. This service might not be available consistently. Generally, it is safer to either move the definition "profiler" to scope "prototype", or declare "container" as a child scope of "prototype". If you can be sure that the other scope is always active, you can set the reference to strict=false to get rid of this error. And it confuses me, because I don't know what to do. I tried to set property "strict=false", but nothing happens.

Original source