我如何可以访问请求我在Laravel 4手动派出查询字符串参数? [英] How can I access query string parameters for requests I've manually dispatched in Laravel 4?

查看:123
本文介绍了我如何可以访问请求我在Laravel 4手动派出查询字符串参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个简单的API,以及建立在这个API上一个简单的Web应用程序。

I'm writing a simple API, and building a simple web application on top of this API.

由于我想消费我自己的API直接,我第一次用Google搜索,发现在计算器上这个答案完全哪个回答我最初的问题:的消费我自己laravel API

Because I want to "consume my own API" directly, I first Googled and found this answer on StackOverflow which answers my initial question perfectly: Consuming my own Laravel API

现在,这个伟大工程,我能够做类似的东西进入我的API:

Now, this works great, I'm able to access my API by doing something like:

$request = Request::create('/api/cars/'.$id, 'GET');
$instance = json_decode(Route::dispatch($request)->getContent());

这是伟大的!但是,我的API还允许您添加一个可选的字段的参数设置为GET查询字符串来指定要返回的特定属性,比如这个:

This is great! But, my API also allows you to add an optional fields parameter to the GET query string to specify specific attributes that should be returned, such as this:

http://cars.com/api/cars/1?fields=id,color

现在我居然在API中处理这个问题的方式就是沿着这个线的东西:

Now the way I actually handle this in the API is something along the lines of this:

public function show(Car $car)
{
     if(Input::has('fields'))
     {
          //Here I do some logic and basically return only fields requested
          ....
     ...
 }

我认为我可以做同样的事情,因为我以前的查询字符串参数少没办法,像这样:

I would assume that I could do something similar as I did with the query string parameter-less approach before, something like this:

$request = Request::create('/api/cars/' . $id . '?fields=id,color', 'GET');
$instance = json_decode(Route::dispatch($request)->getContent());

但是,这似乎并非如此。长话短说,通过$ C $步进c那么它似乎请求对象正确创建(并正确翻出的字段的参数后,和受让人的标识,颜色的吧),路径似乎出动OK,但我的API控制器本身,我不知道如何访问的字段的参数。使用输入::得到('场')(这是我使用的正常的请求)返回任何内容,我敢肯定,这是因为静态输入正在引用或作用域初始请求进来,而不是新的要求我派遣手动从应用程序本身。

BUT, it doesn't seem so. Long story short, after stepping through the code it seems that the Request object is correctly created (and it correctly pulls out the fields parameter and assigns id,color to it), and the Route seems to be dispatched OK, but within my API controller itself I do not know how to access the field parameter. Using Input::get('fields') (which is what I use for "normal" requests) returns nothing, and I'm fairly certain that's because the static Input is referencing or scoping to the initial request the came in, NOT the new request I dispatched "manually" from within the app itself.

所以,我的问题是如何真正的我应该这样做呢?难道我做错了什么?理想情况下,我想避免在API控制器做任何丑陋的或特殊的,我希望能够使用输入::获取内部调度的请求,而不必进行第二次检查等。

So, my question is really how should I be doing this? Am I doing something wrong? Ideally I'd like to avoid doing anything ugly or special in my API controller, I'd like to be able to use Input::get for the internally dispatched requests and not have to make a second check , etc.

推荐答案

您是在使用输入正确实际上是参考当前的请求,而不是新创建的请求。您的意见将可以在你实例化支持::创建请求实例本身()

You are correct in that using Input is actually referencing the current request and not your newly created request. Your input will be available on the request instance itself that you instantiate with Request::create().

如果您正在使用(你应该是)照亮\\ HTTP \\请求来实例化你的要求,那么你可以使用 $请求 - >输入('键') $请求 - 方式>查询('键')得到的查询字​​符串参数

If you were using (as you should be) Illuminate\Http\Request to instantiate your request then you can use $request->input('key') or $request->query('key') to get parameters from the query string.

现在,这里的问题是,你可能没有你的在路由提供给你照亮\\ HTTP \\请求实例。在这里(这样就可以继续使用输入门面)一种解决方法是物理更换当前请求的输入,然后再切换回来。

Now, the problem here is that you might not have your Illuminate\Http\Request instance available to you in the route. A solution here (so that you can continue using the Input facade) is to physically replace the input on the current request, then switch it back.

// Store the original input of the request and then replace the input with your request instances input.
$originalInput = Request::input();

Request::replace($request->input());

// Dispatch your request instance with the router.
$response = Route::dispatch($request);

// Replace the input again with the original request input.
Request::replace($originalInput);

这应该工作(理论上),你仍然应该能够前后内部API请求之后使用你原来的请求输入。

This should work (in theory) and you should still be able to use your original request input before and after your internal API request is made.

这篇关于我如何可以访问请求我在Laravel 4手动派出查询字符串参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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