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

梳丶 发布于 2016/09/18 14:24
阅读 1K+
收藏 0

我写了一个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

通过路由可以执行这个方法的,为什么不能通过命令行来执行呢?

加载中
返回顶部
顶部