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

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

问题描述

我正在编写一个简单的 API,并在此 API 之上构建一个简单的 Web 应用程序.

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

因为我想直接使用我自己的 API",所以我首先用谷歌搜索并在 StackOverflow 上找到了这个答案,它完美地回答了我最初的问题:使用我自己的 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 查询字符串添加可选的 fields 参数以指定应返回的特定属性,例如:

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());

但是,似乎并非如此.长话短说,在逐步执行代码后,似乎 Request 对象已正确创建(并且它正确地提取了 fields 参数并分配了 id,color 到它),并且路由似乎可以正常调度,但是在我的 API 控制器本身中,我不知道如何访问 field 参数.使用 Input::get('fields')(这是我用于正常"请求的方法)不会返回任何内容,而且我很确定这是因为静态 Input正在引用或限定传入的初始请求,而不是我从应用程序本身手动"发送的新请求.

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 控制器中做任何丑陋或特殊的事情,我希望能够将 Input::get 用于内部调度的请求,而不必进行第二次检查等.

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.

推荐答案

您是正确的,使用 Input 实际上是在引用当前请求,而不是您新创建的请求.您的输入将在您使用 Request::create() 实例化的请求实例本身上可用.

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().

如果您使用(应该使用)Illuminate\Http\Request 来实例化您的请求,那么您可以使用 $request->input('key')$request->query('key') 从查询字符串中获取参数.

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.

现在,这里的问题是你可能没有你的 Illuminate\Http\Request 实例在路由中可用.这里的一个解决方案(以便您可以继续使用 Input 外观)是物理替换当前请求上的输入,然后将其切换回来.

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天全站免登陆