0

I'm trying to use the symfony2 service container.However when I construct each service I want to use some of the variables defined in my controller.I want to know whether that's possible and how to do it.or an alternative if the aforementioned is not possible.

p.s: I'm using the yml service definitions.

$_ws['configurator'] = cmfGetInitObject();  //returns an instance of cmlObj
$_ws['configurator']->setOptions(
array(
     'configurationsFolderPath' => realpath(dirname(__FILE__) . '/../configurations'),
     'server' => &$_SERVER,
     'legacyFormatEnabled' => true
)
);
$_ws['packageManager'] =& $_ws['configurator']->packageManager;
$_ws = $_ws['configurator']->load($_ws);   //$_ws['configurator'] again resolves to a cmlObj

this is the array containing the configurations I mentioned.

5
  • 1
    Controllers are request-scoped. And services not defined as such aren't. If you don't instanciate your service in your controller, you might need to set an attribute into your request. What's your use case? What kind of parameter do you need? Isn't there another way to handle this? Commented Apr 7, 2014 at 7:01
  • I'm trying to convert a factory based approach to a service-container approach in the already written website.However It could be handled if I could define an array as a service and pass that through some functions.However I would have to call some functions from other classes/global php code on the array which I don't know is possible or not.which i gather from what you said is not possible.So how can I handle this with setting attributes into my request,and how would that affect the services if they're not request-scoped? Commented Apr 7, 2014 at 7:07
  • Can you show us some code? Commented Apr 7, 2014 at 7:09
  • It's lot of code.but hypothetecilly speaking let's say I have the array foo which contains all the configurations for the website.and I have a mailer class which takes an argument for the constructor.so: $mailer=new Mailer(foo['mailer']); however foo is generated depending on the state.e.g.the user being logged in or not. Commented Apr 7, 2014 at 7:17
  • @Touki I added the code for the configurations array.the cmfGetInitObject() constructs it using a factory. Commented Apr 7, 2014 at 7:38

1 Answer 1

1

You can't. The service container is created at the beginning of the request flow, while the controller is executed almost at the end of the flow.

If you really have not access to the required config in the container build phase of the request, you can use setters to set the values in the container and maybe use default values when building the service.

Even better would be to move this out of the controller and inside an event listener.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm not sure if I don't have the access to the required config in the container build phase.could you clarify a bit of what can and can't be done in the service container?
@user2268997 the container build phase is the very first phase. You've only access to the Request object, nothing more. So if you need the current user, you can't do it in this phase.
Thanks,can you happen to think of a workaround for that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.