2017-12-02 8 views
0

サーバがssh接続をサポートしていないために、コントローラからSymfonyコマンドを実行する必要があります。symfonyコントローラでKernelInterfaceを呼び出してください

私は、このコードは、ほとんどのSymfonyドキュメントの例のようなものです。このsymfonyのドキュメントに https://symfony.com/doc/3.3/console/command_in_controller.html

/** 
* @Route("/command/run") 
* @Method("POST") 
* 
* @param KernelInterface $kernel 
* 
* @return Response 
* @throws \Exception 
*/ 
public function runCommandAction(KernelInterface $kernel) 
{ 
    $application = new Application($kernel); 
    $application->setAutoExit(false); 

    $input = new ArrayInput([ 
     'command' => 'doctrine:schema:update', 
     "--force" => true 
    ]); 

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

    $content = $output->fetch(); 

    return new Response($content); 
} 

を見つけました。

しかし、コードを実行するとこのエラーが発生します。

提供タイプ "のSymfony \コンポーネント\ HttpKernel \ KernelInterfaceは" インタフェースであり、

symfonyのバージョンをインスタンス化することはできませんが、3.3
PHPのバージョンである7.1

である私がいることを追加する必要があります私はFOSRestバンドルを使用していますが、問題ではないはずです。

ここで私は間違っていますか? 何か不足していますか?

+0

services.yamlファイルにエイリアスを作成する必要があると思います。 https://symfony.com/doc/current/service_container/autowiring.html#working-with-interfacesこれはautowireの最近の変更のようなものです。 – Cerad

+0

新しいS4.0プロジェクトでコードを試しましたが、期待通りに機能しました。私はあなたがKernelInterfaceを実装する2つのサービスを持っている必要があると思います。いずれにしても、Symfony \ Component \ HttpKernel \ KernelInterface: '@kernel'をサービスファイルに追加してみてください。 – Cerad

+0

@Ceradありがとう:)これは、私が役立つように答えを加えるために働くかどうかをあなたに通知します:)あなたが望むなら、あなたは今でも追加できます。 –

答えて

0

私はこの問題を、コンストラクトクラスにinterfaceを追加することで解決しました。

/** 
* @var KernelInterface 
*/ 
private $kernel; 

public function __construct(KernelInterface $kernel) 
{ 
    $this->kernel = $kernel; 
} 

/** 
* @Route("/command/run") 
* @Method("POST") 
* 
* @param KernelInterface $kernel 
* 
* @return Response 
* @throws \Exception 
*/ 
public function runCommandAction() 
{ 
    $application = new Application($this->kernel); 
    $application->setAutoExit(false); 

    $input = new ArrayInput([ 
     'command' => 'doctrine:schema:update', 
     "--force" => true 
    ]); 

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

    $content = $output->fetch(); 

    return new Response($content); 
} 
関連する問題