在Laravel 4中呼叫控制器 [英] Call a controller in Laravel 4

查看:81
本文介绍了在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天全站免登陆