Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony: How i can run command from controller?

Tags:

symfony

I want to run a command from controller but nothing happens. When I run a command from the console, everything works - the command starts and imports the database.

I would like a command to run after calling the controller. Below is my controller code. Do I have to define anything else? Routing etc?

<?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);
    }
}
like image 324
scully09 Avatar asked Aug 31 '25 18:08

scully09


1 Answers

Try this, by way of example and working in Symfony4.4:

Change

class ImportController extends AbstractController

to:

class ImportController extends Controller

Add route: config/routes.yaml

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

Example:

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);
    }
}

Execute a request:

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

What's the difference between Controller or AbstractController? Not much: both are identical, except that AbstractController is more restrictive. The $this->get() and $this->container->get() methods don't allow you to access to your own services but only to a limited set of services commonly used in controllers (such as twig, doctrine, session, etc.)

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

like image 139
rescobar Avatar answered Sep 02 '25 21:09

rescobar