symfony一个方法,我写进路由能正常访问,但是通过命令行执行就报错,怎么解决?

weixin_38051598 2016-09-18 02:24:01
我写了一个myComand类: <?php //src/ChannelBundle/Command/MyCommand.php namespace ChannelBundle\Command; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; use ChannelBundle\Controller\CmdController; class MyCommand extends ContainerAwareCommand { protected function configure() { $this->setName('action:run') ->setDescription('Run an action.') ->addArgument('name', InputArgument::OPTIONAL, 'Chouse an action to run.') ->addOption('show', null, InputOption::VALUE_NONE, 'Show result.'); } protected function execute(InputInterface $input, OutputInterface $output) { $action = new CmdController(); $name = $input->getArgument('name'); if ($name == 'action1'){ $text = $action->insertData(); }elseif($name == 'action2') { $text = $action->deleteData(); }elseif($name == 'action3') { $text = $action->updateData(); }elseif($name == 'action4') { $text = $action->seleteData(); }else { $text = 'Error[101]:Cmd error.'; } if (!$input->getOption('show')){ $text = NULL; } $output->writeln($text); } } 可以看出,我通过这个类,创建命令行,执行另一个类:CmdController中的方法:insertData public function insertData() { if($this->get('database_connection')) $str = "Action4 has been executed."; else $str = "Errors[001]:Syntax error!"; return $str; } 但是 $this->get('database_connection') 一直报错:   [Symfony\Component\Debug\Exception\FatalThrowableError]   Call to a member function get() on null 通过路由可以执行这个方法的,为什么不能通过命令行来执行呢?
...全文
39 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

476

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧