【问题标题】:Could not open input file: application.php Symfony CLI无法打开输入文件:application.php Symfony CLI
【发布时间】: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


【解决方案1】:

您收到的错误表明application.php 的路径无效。通过在不存在的文件上调用 PHP 可以重现相同的错误:

$ php doesnotexist.php

输出:

无法打开输入文件:dosnotexist.php


linked to 的文档指的是 Symfony 的控制台组件,但根据您的代码 sn-p,您似乎正在使用整个 Symfony 框架。区别很重要,因为预计组件的功能独立于框架的其余部分,这意味着在使用框架的其余部分时实现可能会略有不同。

我怀疑这篇食谱文章会更有帮助:How to Create a Console Command

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 2014-01-13
    • 2015-12-07
    • 1970-01-01
    相关资源
    最近更新 更多