我正在尝试在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();
您得到的错误表明application.php
的路径无效。同样的错误可以通过在不存在的文件上调用PHP来重现:
$ php doesnotexist.php
输出:
无法打开输入文件:doesnotexist.php
您链接到的文档引用了Symfony的控制台组件,但根据您的代码片段,您似乎在使用整个Symfony框架。这种区别很重要,因为组件的功能应独立于框架的其他部分,这意味着在使用框架的其余部分时,实现可能略有不同。
我怀疑这篇食谱文章会更有帮助:如何创建控制台命令