简体   繁体   中英

How do I call a controller action from within a command in Symfony?

I am working on an application which serves as an API. I have implemented a POST method for adding records to a table in the application.

I also have a command which is to be run daily - is there a way to call my POST API method internally from within my command? Or do I just call it via the application URL?

I am using Symfony 3.4.6.

Appreciate any advice.

You can use the Symfony kernel to execute HTTP requests.

You can use the same kernel of the Console, or create a new one (if you must change environnement).

use App\Kernel;
use Symfony\Component\HttpFoundation\Request;

kernel = new Kernel('prod', false);
$request = Request::create('/toto', 'GET');
$response = $kernel->handle($request, HttpKernelInterface::MASTER_REQUEST, false);

die($response->getContent());

(tested with Symfony 5).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM