1

i am trying to Create a basic Command in Symfony.

So i am following this the cookbook in Symfony.

But where it says Test the new console command by running the following

$ php application.php demo:greet Fabien

there i am always getting an error, says ---

Could not open input file: application.php

i have create **GreetCommand.php** file and copy that exact php commands. and also create a application.php file where i follow the instruction.

i have put those two files in the same directory/folder.

What i am doing wrong and why i am getting that error.

Here is code for **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);
    }
}

here is the code for 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();
1
  • Try: php ./application.php demo:greet Fabien And of course make sure you are in the same directory as your files. Commented Nov 16, 2015 at 22:41

1 Answer 1

1

The error you are getting suggests that the path to application.php is not valid. The same error is reproducible by invoking PHP on a non-existant file:

$ php doesnotexist.php

outputs:

Could not open input file: doesnotexist.php


The documentation you linked to refers to Symfony's console component, but based on your code snippet, you appear to be using the entire Symfony framework. The distinction is important as components are expected to be functional independently from the rest of the framework which means the implementation may differ slightly when using the rest of the framework.

I suspect this cookbook article will be much more helpful: How to Create a Console Command

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.