我在我的Symfony项目中使用了我的供应商文件夹中的一些自定义类。现在,我需要从我的parameters.yml它位于从供应商文件夹Symfony访问parameters.yml
C:\xampp\htdocs\myproject\app\config\parameters.yml
在我的正常Symfony的代码访问某些参数我只是做
$this->getParameter('myparameter');
和所有的设置,而不是在供应商的文件夹中。我想我需要导入一些命名空间,但无法找到哪些? 任何帮助,将不胜感激。谢谢。
UPD1这个问题是通过添加以下代码AppBundle.php
class AppBundle extends Bundle
{
private static $containerInstance = null;
public function setContainer(\Symfony\Component\DependencyInjection\ContainerInterface $container = null)
{
parent::setContainer($container);
self::$containerInstance = $container;
}
public static function getContainer()
{
return self::$containerInstance;
}
}
,然后调用从我的供应商代码的容器解决了以下内容:
use AppBundle\AppBundle;
AppBundle::getContainer()->getParameter('myparameter');
感谢大家帮帮我。
[注入服务容器](http://stackoverflow.com/questions/) 12056178/how-to-access-service-container-in-symfony2-global-helper-function-service)并使用[' - > getParamter()'](http://symfony.com/doc/current/service_container/ parameters.html)? – kero
我是否需要将其定义为服务,或者使用Symfony \ Component \ DependencyInjection \ ContainerInterface作为Container;足够?顺便说一句,这种方法不工作说未定义的属性$容器 – Jack
@杰克您的更新代码正是**不**做。静态从容器中获取参数正在破坏容器的整个目的。供应商文件夹中的类现在将与AppBundle类和容器紧密耦合。请不要这样做。实例化时,通过构造函数将所需参数注入类中。这就是依赖注入容器的用途。我的答案在下面解释。 – daviomey