In a Symfony2 app\'s routing configuration, I can refer to a file like this:
somepage:
prefix: someprefix
resource: \"@SomeBundle/Resources/config/co
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')
}
}