symfony:如何传递上传的bundle自己的路径

时间:2017-07-07 06:59:35

标签: php symfony symfony-3.3

我正在使用symfony 3.3

我已经在我的应用程序中创建了一个我通过composer安装到vendor/的包。我有这个包需要一些yml文件,我通过依赖注入注入它。到目前为止一切都很好。但是我在这个包里面也有一个yml文件,这个包必须知道这个(他自己的)文件的路径。

目前我在我的一个班级中有这个

protected const LOCAL_FILE = __DIR__.'/../Resources/translations/ru.yml';

它工作正常,但我认为这不是一个合适的解决方案。我试着将%kernel.root_dir%传递给这个班级但是从那里我仍然无法猜测整个路径。

我该如何以最好的方式做到这一点?

更新:已经做了什么zerkms建议我得到结果调用此方法的方式如下:

  my.translator:
      ...
      calls:
          - method: configure
            arguments:
               ...
               - '@kernel'

以及locateResource

的部分
 protected const LOCAL_FILE = '@MyBundle/Resources/translations/ru.yml';
  ....
  ....
  $this->kernel->locateResource(self::LOCAL_FILE)

1 个答案:

答案 0 :(得分:1)

您可能使用过Symfony\Component\HttpKernel::locateResource()方法。

内核对象可以通过容器中的@kernel id获得。