简体   繁体   中英

How to use php variables in configuring a service i symfony2?

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.

ps: 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM