Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access query string parameters for requests I've manually dispatched in Laravel 4?

I'm writing a simple API, and building a simple web application on top of this 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

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

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

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

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.

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.

like image 281
Kevin Mitchell Avatar asked May 16 '13 21:05

Kevin Mitchell


People also ask

Can we send query parameters in GET request?

Unless there's something sensitive in the request parameters, it is perfectly fine to send them as part of URL.

How can pass query string in URL in laravel?

You can pass query string to URL in laravel using named route and controller action. You can pass query string as comma separated array to named route and controller action and redirect to URL.


1 Answers

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

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.

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

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.

like image 61
Jason Lewis Avatar answered Sep 21 '22 13:09

Jason Lewis