Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to inject YAML file to a service in Symfony?

I would like to inject a YAML configuration to a service in Symfony. Currently, I inject path of my bundle and a relative path to the file and parse it manually with Yaml::parse but is there an easier way to do this?
So the goal would be that the constructor of the service would receive an array of the already parsed YAML config.


To put some more detail about it:

Currently, I have a YAML file in a Resources directory in my bundle and to parse it in a service, I use something like this:

$yamlFile = sprintf(
        '%s/%s',
        $container->get('kernel')->getBundle('MyBundle')->getPath(),
        $pathToFile
);

So I have to inject both the container (can I somehow inject just the path to the bundle?) and the path to the file. I wouldn't mind restructuring my system a bit, e.g. put the yaml file in the config directory of the bundle, if that would help, but I'd like to keep it separate from other configuration.

like image 631
Czechnology Avatar asked Oct 25 '25 22:10

Czechnology


1 Answers

You can load needed configuration in bundle extension like described there: http://symfony.com/doc/current/cookbook/bundles/extension.html and save it to new parameter, or inject to any service you like.

like image 175
Dmitry Grachikov Avatar answered Oct 28 '25 16:10

Dmitry Grachikov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!