If you are using Laravel to develop PHP websites (you should if you are not using it!) you will usually create your own API. If you create an API, most of the time it is because you will need to call this API from outside your website. But sometimes you want to call your API from your website. And it’s not always easy to call your own API. Let’s see a few common mistakes (it took me 1 hour to figure this out) and how to solve them.
Calling your API with no parameters
No problem here, you can use the following code:
$request = Request::create('/api/page/'.$idPage, 'GET'); $instance = json_decode(Route::dispatch($request)->getContent());
Calling your API with parameters
This is where it gets tricky. Imagine you want to call the following URL:
http://example.com/api/page/1?section=howto
If you change the previous code with something like that:
$request = Request::create('/api/page/'.$idPage.'?section=howto', 'GET'); $instance = json_decode(Route::dispatch($request)->getContent());
And if you try to do something like this in your API:
public function show(Page $page) { if (Input::has('section')) { // code } }
You will not be able to get the section
parameter in your API controller with Input::has('section')
.
But why?
In fact 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 are using Illuminate\Http\Request
in your API, then you can use $request->input('key')
or $request->query('key')
to get parameters from the query string. But you have a problem if you are using the Input
facade in your API.
A solution (so that you can continue using the Input
facade) is to replace the input on the current request, then switch it back.
// Store the original input of the request $originalInput = Request::input(); // Create your request to your API $request = Request::create('/api/page/'.$idPage.'?section=howto', 'GET'); // Replace the input with your request instance input Request::replace($request->input()); // Dispatch your request instance with the router $response = Route::dispatch($request); // Fetch the response $instance = json_decode(Route::dispatch($request)->getContent()); // Replace the input again with the original request input. Request::replace($originalInput);
With this you will be able to use your original request input before and after your internal API request. The Input
facade in your API will be able to fetch the right parameters.
Proper internal requests
You have seen how to make internal requests to your API, but the code is not so beautiful. If you are making only a request sometimes, it is okay to use the previous example. But if you need to do several requests to your API, you will need a cleaner code.
There is a plugin for that: Laravel HMVC, available on GitHub. With it, you will not need to replace the input for your requests to your API. You will be able to do something like that:
// GET Request. API::get('user/1'); // POST Request. API::post('user', array('title' => 'Demo')); // PUT Request. API::put('user/1', array('title' => 'Changed'));
Convenient isn’t it? You can add it to your composer.json
file:
"teepluss/api": "dev-master"