I have a plain PHP class and I want to read one of the parameter from config.yml file. I dont have a container instance to read and get the param value. How can I achieve this in a plain PHP class? Important thing is that I can not make a service of my php class.
1 Answer
It's actually very easy, since YAML is component on it's own:
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser;
$yaml = new Parser();
try {
$yamlData = $yaml->parse(file_get_contents('/path/to/config.yml'));
// adjust what you need to read here.
$paramValue = $yamlData['your_key']['sub_key'];
} catch (ParseException $e) {
printf("Unable to parse the YAML string: %s", $e->getMessage());
}
Source: Reading YAML Files