rgvcorley
rgvcorley

Reputation: 2933

Where to put config file for my own library and have the path injected into Symfony service

Ok, so I have a library that I've written that I'd like to use in Symfony. It has it's configuration stored in a yml file.

At the moment I have the yml file in src/AppBundle/Resources/config. I need the path of this config file to be injected into the service. At the moment I have simply hard coded a relative path to the config file in the services.yml:-

service_name: class: My\Library\Service arguments: ["../src/AppBundle/Resources/config/my_library_config.yml"]

I feel like this isn't the correct way to go about this?

Upvotes: 2

Views: 197

Answers (1)

Anyone
Anyone

Reputation: 2835

You can use the Extension class to generate the path for you and replace the argument when the container compiles.

However, Symfony already has a configuration processing method. The extension option is kind of useless without the configuration though, so if you're not building it to be re-used, use %kernel.root_dir% to prepend the right path.

If you are going to be reusing it, there's no point in making the configuration static. In that case I recommend you to make a configuration option so that people can configure your bundle/options theirself.

Is there an actual reason you want to make your own config though? You could simply define a parameter and store the whole config in a parameter, that saves you from creating and parsing your own configuration file and allows you to benefit from the parameter system.

Upvotes: 1

Related Questions