Skip to content

Instantly share code, notes, and snippets.

@kieranja
Last active January 5, 2023 03:00
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save kieranja/10278326 to your computer and use it in GitHub Desktop.
Save kieranja/10278326 to your computer and use it in GitHub Desktop.
Laravel Batch Request
// Simple. This method allows you to make a request like http://myapi.com/api/batch?request=[{"url":"/api/profile/1", "type":"GET", "request_id":"1"}, {"url":"/api/profile/2", "type":"GET", "request_id":2}]
$requests = \Input::get('request');
$requests = json_decode($requests);
$output = array();
foreach ($requests as $request) {
$url = parse_url($request->url);
$query = array();
if (isset($url['query'])) {
parse_str($url['query'], $query);
}
$req = \Request::create($request->url, $request->type, $query);
if (isset($request->request_id)) {
$output[$request->request_id] = json_decode(\Route::dispatch($req)->getContent());
} else {
$output[] = json_decode(\Route::dispatch($req)->getContent());
}
}
return \Response::json(array('response' => $output));
@malhal
Copy link

malhal commented Aug 18, 2016

Here is a project that expands on this: https://github.com/teepluss/laravel-hmvc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment