Symfony2 service parameters - passing the current

2019-05-11 20:48发布

问题:

I want to create a service in Symfony2 that will take in the current request on the constructor and use it to determine the type of browser used for the request.

I can create the class and define it as a service but how do I pass in the current request using the XML service definition?

回答1:

You should be able to just use the request service. This however requires your service to be declared with scope="request". Another approach i used was to inject the service-container and retrieve the request-Service form there when you need it.