且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Laravel 4 中调用控制器

更新时间:2022-03-27 05:53:44

如果我理解正确,您正在尝试构建一个以 API 为中心的应用程序,并希望在您的 Web 应用程序内部访问该 API 以避免发出额外的 HTTP 请求(例如使用 cURL).对吗?

If I understand right, you are trying to build an API-centric application and want to access the API internally in your web application to avoid making an additional HTTP request (e.g. with cURL). Is that correct?

您可以执行以下操作:

$request = Request::create('api/items', 'GET', $params);
return Route::dispatch($request)->getContent();

请注意,您需要使用通常用于从外部访问 API 的 uri 路由,而不是指定 controller@method 目标.

Notice that, instead of specifying the controller@method destination, you'll need to use the uri route that you'd normally use to access the API externally.

更好的是,您现在可以指定请求应响应的 HTTP 动词.

Even better, you can now specify the HTTP verb the request should respond to.