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

前端 未结 3 1090
甜味超标
甜味超标 2020-12-09 12:31

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 th

3条回答
  •  时光取名叫无心
    2020-12-09 13:19

    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.

提交回复
热议问题