简体   繁体   English

流明 - 调用其他 controller 方法并获得响应

[英]Lumen - Call other controller method and get response

In order to add the ability of bulk API requests to my api I have added a new method to one my controllers.为了向我的 api 添加批量 API 请求的能力,我在我的控制器中添加了一种新方法。
The method takes as input an array of objects and calls another method in the same controller (which may throw an exception).该方法将对象数组作为输入,并在同一 controller 中调用另一个方法(这可能会引发异常)。
see the following code:看下面的代码:

       foreach ($objects as $object) {
            $request = (array)$object;
            $request = Request::create('/someinternalurl', 'POST', $request);
            $response = app()->dispatch($request);
            $responses[] = json_decode($response->getContent());
        }

the problem is that for 10 objects for example, this code takes about 15 seconds.问题是,例如,对于 10 个对象,此代码大约需要 15 秒。
In addition i have tried to search for other solutions, but all i found seems to result另外我试图寻找其他解决方案,但我发现的似乎都是结果
the same respone time or many changes to my API.相同的响应时间或对我的 API 的许多更改。

Thank you谢谢

It's better to use method directly, rather than sending request to corresponding route.最好直接使用方法,而不是向相应的路由发送请求。 For example:例如:

$response = (new YourController())->yourMethod($object);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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