dongyihao9887
2017-02-28 09:06
浏览 37
已采纳

Symfony:从控制器外部读取parameter.yml

I Want to read a pareameter from parameters.yml. the solution is to extend the Controller but I got this error :

Error: Call to a member function getParameter() on null

I know that the container is null, but i don't know how to get the container?

class Configuration extends Controller
{
    public function __construct()
   {
     $this->tempFolderPath = sys_get_temp_dir();
     $parameter = $this->container->getParameter('a');
   }
}

Any solution ? Any help ?

图片转代码服务由CSDN问答提供 功能建议

我想从parameters.yml读取一个pareameter。 解决方案是扩展Controller但我收到此错误 :

 错误:在null上调用成员函数getParameter()  
 
 

我知道容器是 null,但我不知道如何获取容器?

  class Configuration extends Controller 
 {
 public function __construct()
 {
 $ this-  > tempFolderPath = sys_get_temp_dir(); 
 $ parameter = $ this-> container-> getParameter('a'); 
} 
} 
   
 
 <  p>任何解决方案? 有什么帮助吗? 
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duandong7980 2017-02-28 10:24
    已采纳

    this is not a best practice, but for me this is the only solution :( because in my code I have a static method that instantiate the hole class. so the solution will be.

    public function __construct()
    {
        $this->tempFolderPath = sys_get_temp_dir();
        global $kernel;
        $this->host = $kernel->getContainer()->getParameter('ws_host');
    }
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题