jacquesndl jacquesndl - 10 months ago 259
PHP Question

Console/Symfony Call command from controller

I have some issues with Console when I try to call a Command from a Controller.
I found a way in the Symfony CookBook :

It seems doesn't work... Maybe I forgot something!

Command :

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()
->setDescription('Greet someone')
'Who do you want to greet?'
'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);


(The command works well in cmd)

Controller :

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;

class MainController extends Controller
public function indexAction()
$kernel = $this->get('kernel');
$application = new Application($kernel);

$input = new ArrayInput(array(
'command' => 'demo:greet'

$output = new BufferedOutput();
$application->run($input, $output);

$content = $output->fetch();



[Symfony\Component\Console\Exception\CommandNotFoundException] There are no commands defined in the "demo" namespace.

Thank you in advance for your help.


You used the wrong Application. You should replace use Symfony\Component\Console\Application; by use Symfony\Bundle\FrameworkBundle\Console\Application;