[英]How do I call a controller action from within a command in Symfony?
I am working on an application which serves as an API.我正在开发一个用作 API 的应用程序。 I have implemented a POST method for adding records to a table in the application.
我已经实现了一个 POST 方法,用于将记录添加到应用程序中的表中。
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?我还有一个每天运行的命令 - 有没有办法从我的命令内部调用我的 POST API 方法? Or do I just call it via the application URL?
或者我只是通过应用程序 URL 调用它?
I am using Symfony 3.4.6.我正在使用 Symfony 3.4.6。
Appreciate any advice.感谢任何建议。
你可以实现 symfony/console 组件: https : //symfony.com/doc/current/components/console.html https://symfony.com/doc/current/console.html
You can use the Symfony kernel
to execute HTTP requests.您可以使用 Symfony
kernel
来执行 HTTP 请求。
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). (用 Symfony 5 测试)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.