How can I run symfony 2 run command from controller

前端 未结 9 1458
长发绾君心
长发绾君心 2020-11-28 20:03

I\'m wondering how can I run Symfony 2 command from browser query or from controller.

Its because I don\'t have any possibility on hosting to run it

9条回答
  •  伪装坚强ぢ
    2020-11-28 20:13

    Register your command as a service and don't forget to call setContainer

    MyCommandService:
        class: MyBundle\Command\MyCommand
        calls:
            - [setContainer, ["@service_container"] ]
    

    In your controller, you'll just have to get this service, and call the execute method with the rights arguments

    Set the input with setArgument method:

    $input = new Symfony\Component\Console\Input\ArgvInput([]);
    $input->setArgument('arg1', 'value');
    $output = new Symfony\Component\Console\Output\ConsoleOutput();
    

    Call the run method of the command:

    $command = $this->get('MyCommandService');
    $command->run($input, $output);
    

提交回复
热议问题