如何从前端按钮放置控制台命令?

I am a newbie in Symfony. Anyhow I have found my way to build up a Console Command. So this command needs to be accessible from the frontend.

So by my opinion, I need to put the command to service. I have followed this link. So this should be created. But now I don't know how to connect this service to the actual route call. I have formed a route like this:

command:
    path:               /command
    defaults:
        _controller:    AppBundle:Command:activate
    requirements:
        language:       '%pimc.akeneo_cms.frontend.language.available%'

And I have created new controller called CommandController with just one method called activateAction(). And I don't know what to put in actiavateAction ?

Could someone help me ? Am I on a right path ?

You can follow these steps:

  • create the console command
  • create the controller with an action method and a route
  • create a twig template to be rendered from the controller's method action
  • inside the template, create a button
  • using ajax, on button click, make a request into that controller's method action (or another method action)
  • inside the method, call the console command as here is explained

If you want to run a command in your controller's action, you can use Application:

$application = new Application($this->get('kernel'));
$input = new ArrayInput(array('command' => 'your:command'));
$output = new BufferedOutput();
$application->run($input, $output);

And if you want to check the output of the command you can use $output->fetch().