我想从parameters.yml读取一个pareameter.解决方案是扩展Controller,但我收到了这个错误:
Error: Call to a member function getParameter() on null
Run Code Online (Sandbox Code Playgroud)
我知道容器是null,但我不知道如何获取容器?
class Configuration extends Controller
{
public function __construct()
{
$this->tempFolderPath = sys_get_temp_dir();
$parameter = $this->container->getParameter('a');
}
}
Run Code Online (Sandbox Code Playgroud)
有解决方案吗 有帮助吗?
在扩展Controller调用的类中,$this->container->getParameter('a')因为Container被注入到这些类中.当您在其他课程中时,您需要通过将您的课程定义为服务来注入您想要的参数.
您的服务定义如下所示:
services:
class: App\Your\FooClass
arguments:
- %a%
Run Code Online (Sandbox Code Playgroud)
注意%- 注入参数是特殊字符
然后你的班级将如下:
class FooClass
{
protected $a;
public function __construct($a)
{
$this->a = $a;
//you have your "a" parameter value injected into constructor
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1698 次 |
| 最近记录: |