Symfony2 ScopeCrossingInjectionException when you use prototype scope

Go To StackoverFlow.com

11

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.

2012-04-04 08:33
by maectpo
@mactpo have you found solution for this - senK 2014-06-26 11:50


6

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

2014-06-17 07:00
by Andy Rosslau


1

Container scope since Symfony 2.8 was deprecated.

scope: prototype was replaced by shared: false

http://symfony.com/doc/2.8/cookbook/service_container/shared.html

2016-05-16 07:15
by Aistis
Ads