我在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);
}
}
?>
Run Code Online (Sandbox Code Playgroud)
并试图通过它来称呼它
app/console维护:问候Fabien
但我总是得到以下错误:
[InvalidArgumentException]"maintenance"命名空间中没有定义命令.
有任何想法吗?
Sam*_*Sam 55
我有这个问题,这是因为我的PHP类和文件的名称没有结束Command.
Symfony将自动注册以bundle 结尾Command并Command位于bundle目录中的命令.如果您想手动注册命令,这本食谱条目可能有所帮助:http://symfony.com/doc/current/cookbook/console/commands_as_services.html
Mon*_*roM 20
我遇到了类似的问题并想出了另一种可能的解决方案:
如果覆盖默认__construct方法,Symfony将不会自动注册Command,因此您必须采用前面提到的服务方法或删除__construct覆盖并在execute方法或configure方法中执行init步骤.
有没有人知道如何在Symfony命令中执行init"stuff"的最佳实践?
我花了一点时间才弄明白这一点.
pre*_*ldt 16
我弄清楚它为什么不起作用:我只是忘了在AppKernel.php中注册Bundle.但是,其他建议的答案是相关的,可能有助于解决其他情况!
按照惯例:命令文件需要驻留在bundle的命令目录中,并且名称以Command结尾.
在AppKernel.php中
public function registerBundles()
{
$bundles = [
...
new MaintenanceBundle\MaintenanceBundle(),
];
return $bundles;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22218 次 |
| 最近记录: |