Symfony 2.4 execute Command from Controller
Asked Answered
F

3

2

I want to execute the command fos:elastica:populate from my controller.

I tried that code but it doesn't work, i get error = 1 the var_dump show ""

$command = 'fos:elastica:populate';
$app = new Application($this->get('kernel'));
$app->setAutoExit(false);
$input = new StringInput($command);
$output = new ConsoleOutput;
$error = $app->run($input, $output);
var_dump($error);
var_dump(stream_get_contents($output->getStream());

Any ideas ?

I try a different code.....

    $command = $this->get('FosElasticaPopulateService');
    $input = new StringInput('');

    $output = new ConsoleOutput();
    ladybug_dump($input);
    // Run the command
    $retval = $command->run($input, $output);

    if(!$retval)
    {
        echo "Command executed successfully!\n";
    }
    else
    {
        echo "Command was not successful.\n";
    }
    var_dump(stream_get_contents($output->getStream()));

It say: 'The "no-interaction" option does not exist.' at Input ->getOption ('no-interaction') in the PopulateCommand.

if i change my code with:
$input = new StringInput('--no-interaction');

It say: 'The "--no-interaction" option does not exist.' at
'ArgvInput ->addLongOption ('no-interaction', null) '

Fountainhead answered 20/3, 2014 at 13:55 Comment(0)
T
3

Step by step, how to run cache clear command: app/console cac:cle --env=(current_env) from controller.

At first, make sure to register command as service (service.yml):

xxx.cache.clear:
    class: Symfony\Bundle\FrameworkBundle\Command\CacheClearCommand
    calls:
        - [setContainer, ["@service_container"] ]

Conde in your controller:

$command = $this->container->get('xxx.cache.clear');
$input = new ArgvInput(array('--env=' . $this->container->getParameter('kernel.environment')));
$output = new ConsoleOutput();
$command->run($input, $output);

That's all. | tested on symfony 2.4

Televisor answered 20/3, 2014 at 15:37 Comment(2)
With Symfony\Bundle\FrameworkBundle\Command\CacheClearCommand ot work (I just have one error with a session active but never mind. With FOS\ElasticaBundle\Command\PopulateCommand, it still say The "no-interaction" option does not exist. Is there a way to give the option to FOS ?Fountainhead
@Sancho, all the options you'd like to pass, you need to add into $input var. so it will be like ```$input = new ArgvInput(array('--env=' . $this->container->getParameter('kernel.environment'), '-n'));Televisor
A
2

First of all: check if that command is already registered as a service. If not register it yourself

FosElasticaPopulateService:
    class: Path\To\Bundle\Class
    calls:
        - [setContainer, ["@service_container"] ]

then into your controller

$output = new ConsoleOutput;
$command = $this->get('FosElasticaPopulateService');
$command->run(null, $ouput); //null here is for input parameters; if you need them, insert

if it is already registered, just use it as showed above


Ok, I got it: my solution is an alternative one (maybe more suitable for your own command if bundle's command aren't registered as services natively). Reading some documentation, your method should work aswell. You've got an error that blocks you:

$output = new ConsoleOutput; should be $output = new ConsoleOutput();

Albino answered 20/3, 2014 at 14:6 Comment(7)
Now i have: $output = new ConsoleOutput; $input = new ArrayInput(array("fos:elastica:populate")); $command = $this->get('FosElasticaPopulateService'); $command->run($input, $output);. It doesn't work. I have the symfony error: "The "0" argument does not exist"Fountainhead
Same error with: $output = new ConsoleOutput(); $command = $this->get('FosElasticaPopulateService'); $input = new ArrayInput(array("fos:elastica:populate")); $command->run($input, $output); . There is no "easy" way to execute a simple Command ? Something like SymfonyExecute('fos:elastica:populate'); ?Fountainhead
@Sancho: what are you doing with get('FosElasticaPopulateService');? I told you, them are two alternative solutions. Your code has a typo man, don't mix and mess all the things up please :)Albino
i don't understand something... I use $output = new ConsoleOutput; $command = $this->get('FosElasticaPopulateService'); $command->run(null, $output); Is that correct ? It say 'run() must be an instance of InputInterface.Fountainhead
@Sancho: Sorry if I'm rude but ... Can you read what I wrote? You got a typo! You have to modify $output = new ConsoleOutput in $output = new ConsoleOutput() Please check under the "line" in my answerAlbino
I try my first code with '$output = new ConsoleOutput();'. I got return = 1 (command failed)Fountainhead
@Sancho: so, maybe, you got an error in command or command running. But trust me, this is the right way ;)Albino
A
0

If you need the code in the command to be executed in the controller, put the code in its own class and call that class in the controller and the command.

Aridatha answered 20/3, 2014 at 14:21 Comment(3)
I want to execute a command give by a bundle. I can't move the code from fos:elastica to another class.Fountainhead
I see. The command you are trying to execute has some required options. github.com/FriendsOfSymfony/FOSElasticaBundle/blob/master/… May be you should set them through the input.Aridatha
The option "batch-size" is required and has no default value.Aridatha

© 2022 - 2024 — McMap. All rights reserved.