有一种更好的方式来做到这一点:
$this->container->get('kernel')->locateResource('@AcmeDemoBundle')
将给予AcmeDemoBundle
$this->container->get('kernel')->locateResource('@AcmeDemoBundle/Resource')
会给道路资源的绝对路径在AcmeDemoBundle内的目录,等等......
如果这样的目录/文件不存在,将抛出InvalidArgumentException。
此外,在一个容器定义,你可以使用:
my_service:
class: AppBundle\Services\Config
arguments: ["@=service('kernel').locateResource('@AppBundle/Resources/customers')"]
编辑
你的服务没有依赖内核。您可以使用默认的symfony服务:file_locator。它在内部使用Kernel :: locateResource,但在测试中更容易加倍/模拟。
服务定义
my_service:
class: AppBundle\Service
arguments: ['@file_locator']
类
namespace AppBundle;
use Symfony\Component\HttpKernel\Config\FileLocator;
class Service
{
private $fileLocator;
public function __construct(FileLocator $fileLocator)
{
$this->fileLocator = $fileLocator;
}
public function doSth()
{
$resourcePath = $this->fileLocator->locate('@AppBundle/Resources/some_resource');
}
}
./web/bundles/mybundle可以是一个符号链接到真正./mybundle/ressources/public文件夹,你可能有兴趣获得此代替路径。 –
另外,我建议你从一个干净的服务定义的参数(例如“上传器”服务,而不是)设置$ directoryPath,这是symfony的方式。 –