-1

I need to display a yaml file as an array but i but I can't display it.

I have create a service, and my controller call this service.

In my service i try to call my yaml like this :

$value = Yaml::parseFile('public\assets\organizations.yaml');
return $value;

But that return me that error :

File "public\assets\organizations.yaml" does not exist.
2
  • What have you tried to resolve the problem? That looks like a relative file reference - probably you need to use an absolute path?
    – Nico Haase
    Jun 30, 2021 at 13:18
  • Do a dump(getcwd()); and see what the path is, then match the path to the file using that: getcwd().'/rest/of/the/path.yaml'. Jun 30, 2021 at 13:34

1 Answer 1

2

You are specifying the file with a relative path. This will not be resolved relative to the file it is in but relative to the current working directory you are in when executing the script. Since this depends on various factors it will always be troublesome.

Thus you should always use absolute paths. In symfony you can get the base path of your project via the kernel.project_dir configuration parameter. Your code does not provide enough context to understand how or where you are using it. But if it is inside a controller extending AbstractController you can use getParameter():

$projectDir = $this->getParameter('kernel.project_dir');
$absolutePath = $projectDir . '/public/assets/organizations.yml';

$value = Yaml::parseFile($absolutePath);
return $value;

Also note that using \ as the directory separator won't work under Linux/Unix-like systems where / is used as directory separator! Since / will work as directory separator under Windows, too, it is easiest to use it for cross-OS compatibility. Alternatively use the DIRECTORY_SEPARATOR constant.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.