2

コントローラーからコマンド fos:elastica:populate を実行したいと思います。

そのコードを試してみましたが、うまくいきません。エラー = 1 の 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());

何か案は ?

別のコードを試してみます.....

    $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()));

それは言う:「「相互作用なし」オプションは存在しません。PopulateCommand の Input ->getOption ('no-interaction') で。

コードを次のように変更した場合:
$input = new StringInput('--no-interaction');

「「--no-interaction」オプションは存在しません」と表示されます。at
'ArgvInput ->addLongOption ('no-interaction', null) '

4

3 に答える 3

3

ステップごとに、コントローラーからキャッシュ クリア コマンドを実行する方法: app/console cac:cle --env=(current_env)。

まず、コマンドをサービス (service.yml) として登録します。

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

コントローラーで次のようにします。

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

それで全部です。| | symfony 2.4でテスト済み

于 2014-03-20T15:37:36.057 に答える
2

まず、そのコマンドが既にサービスとして登録されているかどうかを確認します。なければ自分で登録

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

次に、コントローラーに

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

すでに登録されている場合は、上記のように使用してください


わかりました: 私の解決策は別の方法です (バンドルのコマンドがサービスとしてネイティブに登録されていない場合は、独自のコマンドに適している可能性があります)。いくつかのドキュメントを読むと、あなたの方法もうまくいくはずです。あなたをブロックするエラーがあります:

$output = new ConsoleOutput;する必要があります$output = new ConsoleOutput();

于 2014-03-20T14:06:47.210 に答える
0

コマンド内のコードをコントローラーで実行する必要がある場合は、コードを独自のクラスに配置し、コントローラーとコマンドでそのクラスを呼び出します。

于 2014-03-20T14:21:40.280 に答える