Symfony:我如何从控制器运行命令?

问题描述 投票:0回答:2

我想从控制器运行命令,但没有任何反应。 当我从控制台运行命令时,一切正常 - 命令启动并导入数据库。

我希望在调用控制器后运行命令。 下面是我的控制器代码。我还需要定义其他东西吗?路由等?

<?php

namespace App\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

// Import the required classed
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;

/**
* @Route("/api/import")
*/
class ImportController extends AbstractController
{
    /**
     * @Route("/upload")
     */
    public function upload()
    {
        $kernel = $this->get('kernel');
        $application = new Application($kernel);
        $application->setAutoExit(false);

        $input = new ArrayInput(array(
            'command' => 'app:import-database'
        ));

        $output = new BufferedOutput();

        $application->run($input, $output);

        // return the output
        $content = $output->fetch();

        // Send the output of the console command as response
        return new Response($content);
    }
}
symfony
2个回答
4
投票

尝试一下,通过示例并在 Symfony4.4 中工作:

改变

class ImportController extends AbstractController

至:

class ImportController extends Controller

添加路由:config/routes.yaml

import_upload:
  methods: POST
  path: /api/import/upload
  controller: App\ApiBundle\Controller\ImportController::upload

示例:

use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;

class ImportController extends Controller
{
    /**
     * @return Response
     * @throws Exception
     */
    public function upload(): Response
    {
        $kernel = $this->get('kernel');
        $application = new Application($kernel);
        $application->setAutoExit(false);
        $input = new ArrayInput(array(
            'command' => 'app:import-database'
        ));
        $output = new BufferedOutput();
        $application->run($input, $output);
        // return the output
        $content = $output->fetch();
        // Send the output of the console command as response
        return new Response($content);
    }
}

执行请求:

 curl --location --request POST 'http://localhost:8002/api/import/upload' --header 'Content-Type: application/json'

Controller 和 AbstractController 有什么区别?不是 much:两者相同,只是 AbstractController 更多 限制性的。

$this->get()
$this->container->get()
方法不允许您访问自己的服务,而只能访问 控制器中常用的有限服务集(例如
twig
doctrine
session
等)

https://github.com/symfony/symfony-docs/issues/9926


0
投票
    $kernel = $this->get('kernel');
    // Create the application
    $application = new Application($kernel);
   
    $command = new DemoCommand(); // Create object of command
    $application->add($command);
    $input = new ArrayInput(
        [
            'command' => 'demo:create',
            '--arg' => 'test'
        ]
    );
    $command->run($input, new ConsoleOutput());
© www.soinside.com 2019 - 2024. All rights reserved.