在 Laravel 4 中调用控制器 [英] Call a controller in Laravel 4

查看:25
本文介绍了在 Laravel 4 中调用控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Laravel 3 中,您可以使用 Controller::call 方法调用控制器,如下所示:

In Laravel 3, you could call a controller using the Controller::call method, like so:

Controller::call('api.items@index', $params);

我查看了 L4 中的 Controller 类,发现这个方法似乎取代了旧方法:callAction().虽然它不是静态方法,但我无法让它工作.可能不是正确的方法?

I looked through the Controller class in L4 and found this method which seems to replace the older method: callAction(). Though it isn't a static method and I couldn't get it to work. Probably not the right way to do it?

如何在 Laravel 4 中做到这一点?

How can I do this in Laravel 4?

推荐答案

如果我理解正确,您正在尝试构建一个以 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.

这篇关于在 Laravel 4 中调用控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆