Accessing Files Relative to Bundle in Symfony2

PhpSymfony

Php Problem Overview


In a Symfony2 app's routing configuration, I can refer to a file like this:

somepage:
    prefix: someprefix
    resource: "@SomeBundle/Resources/config/config.yml"

Is there any way to access a file relative to the bundle within a controller or other PHP code? In particular, I'm trying to use a Symfony\Component\Yaml\Parser object to parse a file, and I don't want to refer to that file absolutely. Essentially, I want to do this:

$parser = new Parser();
$config = $parser->parse( file_get_contents("@SomeBundle/Resources/config/config.yml") );

I've checked out the Symfony\Component\Finder\Finder class, but I don't think that's what I'm looking for. Any ideas? Or maybe I'm completely overlooking a better way of doing this?

Php Solutions


Solution 1 - Php

As a matter of fact, there is a service you could use for this, the kernel ($this->get('kernel')). It has a method called locateResource().

For example:

$kernel = $container->getService('kernel');
$path = $kernel->locateResource('@AdmeDemoBundle/path/to/file/Foo.txt');

Solution 2 - Php

Thomas Kelley's answer is good (and works!) but if you are using dependency injection and/or don't want to tie your code directly to the kernel, you're better off using the FileLocator class/service:

$fileLocator = $container->get('file_locator');
$path = $fileLocator->locate('@MyBundle/path/to/file.txt')

$fileLocator will be an instance of \Symfony\Component\HttpKernel\Config\FileLocator. $path will be the full, absolute path to the file.

Even though the file_locator service itself uses the kernel, it's a much smaller dependency (easier to substitute for your own implementation, use test doubles, etc.)

To use it with dependency injection:

# services.yml

services:
    my_bundle.my_class:
        class: MyNamespace\MyClass
        arguments:
            - @file_locator

# MyClass.php

use Symfony\Component\Config\FileLocatorInterface as FileLocator;

class MyClass
{
    private $fileLocator;
    
    public function __construct(FileLocator $fileLocator)
    {
        $this->fileLocator = $fileLocator;
    }

    public function myMethod()
    {
        $path = $this->fileLocator->locate('@MyBundle/path/to/file.txt')
    }
}

Solution 3 - Php

You can use $container->getParameter('kernel.root_dir') to get the app folder of your application, and browse your directories to the file you want.

Solution 4 - Php

If you want to do that in a file located in src/.../SomeBundle/... you can use __DIR__ to get the full path of the current file. Then append your Resources/... path to that like

$foo = __DIR__.'/Resources/config/config.yml';

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThomas KelleyView Question on Stackoverflow
Solution 1 - PhpkgildenView Answer on Stackoverflow
Solution 2 - PhpfazyView Answer on Stackoverflow
Solution 3 - PhpReuvenView Answer on Stackoverflow
Solution 4 - PhpprehfeldtView Answer on Stackoverflow