我在src / MaintenanceBundle / Command中创建了一个新类,将其命名为GreetCommand.php,并将以下代码放入其中:
<?php
namespace SK2\MaintenanceBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class GreetCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('maintenance:greet')
->setDescription('Greet someone')
->addArgument('name', InputArgument::OPTIONAL, 'Who do you want to greet?')
->addOption('yell', null, InputOption::VALUE_NONE, 'If set, the task will yell in uppercase letters')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('name');
if ($name) {
$text = 'Hello '.$name;
} else {
$text = 'Hello';
}
if ($input->getOption('yell')) {
$text = strtoupper($text);
}
$output->writeln($text);
}
}
?>
并尝试通过
应用程序/控制台维护:欢迎Fabien
但是我总是得到以下错误:
[InvalidArgumentException]在“维护”名称空间中没有定义任何命令。
有任何想法吗?
我弄清楚了为什么它不起作用:我只是忘了在AppKernel.php中注册Bundle。但是,其他建议的答案也很重要,可能有助于解决其他情况!
按照惯例:命令文件必须位于捆绑软件的命令目录中,并且名称以Command结尾。
在AppKernel.php中
public function registerBundles()
{
$bundles = [
...
new MaintenanceBundle\MaintenanceBundle(),
];
return $bundles;
}