Jie*_*eng 12 console-application symfony
在configure()函数中,我试图获取服务容器
class SetQuotaCommand extends ContainerAwareCommand {
protected function configure() {
parent::configure();
die(get_class($this->getContainer()));
Run Code Online (Sandbox Code Playgroud)
PHP Fatal error: Call to a member function getKernel() on a non-object in ...\Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand.php on line 37
我需要做一些与众不同的事吗?
UPDATE
我注意到,如果我把它工作getContainer在execute().但我不知道,如果它能够getContainer()在configure().我想获取一个配置参数,用于addOption默认值参数.
否则我可以使用
$param1 = $input->getOption('param1') ? : $container->getParameter('param1');
Run Code Online (Sandbox Code Playgroud)
哪个看起来更不直观?
Nob*_*obu 20
$this->container = $this->getApplication()->getKernel()->getContainer();在execute()中调用.请参阅/sf/answers/526246241/
看来服务容器尚未在 中初始化configure。我可以在 中访问它execute。
至于控制台选项的默认值,我可以使用类似的东西
$opt1 = $input->getOption('opt1') ? : $default;
Run Code Online (Sandbox Code Playgroud)
在许多情况下,Symfony2 允许您在检索参数/变量时设置默认值。因此,上述解决方案的快捷方式就是:
$opt1 = $input->getOption('opt1', $default);
Run Code Online (Sandbox Code Playgroud)
只是将其作为答案,这样如果没有其他答案,我就可以关闭它。
| 归档时间: |
|
| 查看次数: |
25193 次 |
| 最近记录: |