Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ScopeWideningInjectionException with setter injection #8307

Closed
@Kezino

Description

@Kezino

Greetings!

Tried to implement the new Symfony 2.3 setter injection method as per documentation:

namespace Acme\HelloBundle\Mail;

use Symfony\Component\HttpFoundation\Request;

class Mailer
{
    protected $request;

    public function setRequest(Request $request = null)
    {
        $this->request = $request;
    }

    public function sendEmail()
    {
        if (null === $this->request) {
            // throw an error?
        }

        // ... do something using the request here
    }
}

and added the service definition as follows:

services:
    greeting_card_manager:
        class: Acme\HelloBundle\Mail\Mailer
        calls:
            - [setRequest, ['@?request']]

But Symfony throws the error:

ScopeWideningInjectionException: Scope Widening Injection detected: The definition "greeting_card_manager" references the service "request" which belongs to a narrower scope. Generally, it is safer to either move "greeting_card_manager" to scope "request" or alternatively rely on the provider pattern by injecting the container itself, and requesting the service "request" each time it is needed. In rare, special cases however that might not be necessary, then you can set the reference to strict=false to get rid of this error.

Am I missing something? Are the options mandatory as well?

scope: request
synthetic: true
synchronized: true

Thanx in advance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions