Tôi tạo ra một lớp mới trong src/MaintenanceBundle/Command, đặt tên nó là GreetCommand.php và đặt đoạn mã sau vào nó:Symfony2 tùy chỉnh giao diện điều khiển lệnh không làm việc
<?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);
}
}
?>
Và cố gắng gọi nó qua
app/console maintenance:greet Fabien
Nhưng tôi luôn luôn nhận được lỗi sau:
[InvalidArgumentException] There are no commands defined in the "maintenance" namespace.
Bất kỳ ý tưởng?
Bạn có xóa bộ nhớ cache sau khi tạo lệnh không? –