概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 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.

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务