php 在 Symfony2 中访问相对于 Bundle 的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7585474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing Files Relative to Bundle in Symfony2
提问by Thomas Kelley
In a Symfony2 app's routing configuration, I can refer to a file like this:
在 Symfony2 应用程序的路由配置中,我可以引用这样的文件:
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:
有没有办法访问与控制器或其他 PHP 代码中的包相关的文件?特别是,我正在尝试使用 Symfony\Component\Yaml\Parser 对象来解析文件,并且我不想绝对引用该文件。基本上,我想这样做:
$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?
我已经查看了 Symfony\Component\Finder\Finder 类,但我认为这不是我要找的。有任何想法吗?或者,也许我完全忽略了这样做的更好方法?
回答by gilden
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()
.
事实上,有一种服务可以用于此目的,即内核 ( $this->get('kernel')
)。它有一个方法叫做locateResource()
.
For example:
例如:
$kernel = $container->getService('kernel');
$path = $kernel->locateResource('@AdmeDemoBundle/path/to/file/Foo.txt');
回答by fazy
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:
Thomas Kelley 的回答很好(并且有效!)但是如果您正在使用依赖注入和/或不想将您的代码直接绑定到内核,那么最好使用 FileLocator 类/服务:
$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.
$fileLocator
将是 的一个实例\Symfony\Component\HttpKernel\Config\FileLocator
。$path
将是文件的完整绝对路径。
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.)
尽管file_locator
服务本身使用内核,但它的依赖性要小得多(更容易替代您自己的实现、使用测试替身等)
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')
}
}
回答by Reuven
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.
您可以使用$container->getParameter('kernel.root_dir')
获取app
您的应用程序的文件夹,并浏览您的目录到您想要的文件。
回答by prehfeldt
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
如果您想在位于的文件中执行此操作,则src/.../SomeBundle/...
可以使用__DIR__
获取当前文件的完整路径。然后将您的Resources/...
路径附加到
$foo = __DIR__.'/Resources/config/config.yml';