【发布时间】:2015-11-17 05:50:37
【问题描述】:
我正在尝试在 Symfony 中创建一个基本命令。
所以我正在关注Symfony 中的食谱。
但是它说通过运行以下命令来测试新的控制台命令
$ php application.php demo:greet Fabien
我总是遇到错误,说 ---
无法打开输入文件:application.php
我已经创建了**GreetCommand.php** 文件并复制了准确的 php 命令。并按照说明创建一个 application.php 文件。
我已将这两个文件放在同一个目录/文件夹中。
我做错了什么以及为什么会出现该错误。
这是**GreetCommand.php**的代码---
<?php
namespace AppBundle\Command;
use Symfony\Component\Console\Command\Command;
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 Command
{
protected function configure()
{
$this
->setName('demo: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);
}
}
这里是 application.php 的代码 ---
#!/usr/bin/env php
<?php
// application.php
require __DIR__.'/vendor/autoload.php';
use AppBundle\Command\GreetCommand;
use Symfony\Component\Console\Application;
$application = new Application();
$application->add(new GreetCommand());
$application->run();
【问题讨论】:
-
试试:php ./application.php demo:greet Fabien 当然要确保你和你的文件在同一个目录中。
标签: php symfony command-line-interface