I'm creating an API to access my blog entries, and rather than Laravel model binding returning a 404 automatically, I'd rather return a 200, and some JSON in the same format as my API controller returns.
I use the code below, which when returning the JSON response works within controllers, but I can't get it working within a Route::bind, instead the JSON output is as follows:
{"success":true,"data":{"headers":{}}}
Here is my code within app/global.php
Route::group(array('prefix' => '/api/blog'),function() {
Route::bind('blog', function($id) {
$Blog = Blog::find($id);
if (!$Blog) {
return Response::json(array('status' => false, 'message' => 'Blog not found'), 200);
}
});
Route::get('index', array('uses' => 'BlogController@index'));
Route::get('create', array('uses' => 'BlogController@create'));
Route::get('read/{blog}', array('uses' => 'BlogController@read'));
});
It even does this if I try and return an empty array in the JSON response like so:
return Response::json(array(), 200);