发布于 2015-08-27 16:39:39 | 124 次阅读 | 评论: 0 | 来源: 网络整理
The Console component will always run the ListCommand
when no command name is
passed. In order to change the default command you just need to pass the command
name to the setDefaultCommand
method:
namespace AcmeConsoleCommand;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
class HelloWorldCommand extends Command
{
protected function configure()
{
$this->setName('hello:world')
->setDescription('Outputs 'Hello World'');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Hello World');
}
}
Executing the application and changing the default Command:
// application.php
use AcmeConsoleCommandHelloWorldCommand;
use SymfonyComponentConsoleApplication;
$command = new HelloWorldCommand();
$application = new Application();
$application->add($command);
$application->setDefaultCommand($command->getName());
$application->run();
Test the new default console command by running the following:
$ php application.php
This will print the following to the command line:
Hello World
小技巧
This feature has a limitation: you cannot use it with any Command arguments.