Reputation: 148
I have created CRUD application using laravel 5.3 in XAMPP following this tutorial now I have to create RESTful API of this CRUD application so that I was able to perform CRUD operation from android app. anyone who helps Thanks in advance
Upvotes: 1
Views: 798
Reputation: 87719
The difference between API calls and a normal HTML app are basically on the response, usually your controllers respond with views(), so they can be rendered:
/// Get the data
$books=Book::all();
/// HTML response
return view('books.index',compact('books'));
An API usually responds with JSON, which in Laravel is as easy as doing
/// Get the data
$books=Book::all();
/// JSON response
return response()->json($books);
or as simple as
return Book::all();
or
return Book::all()->toJson();
Another thing you have to think about in your app architecture are the routes, to differentiate web from api, I usually create my endpoints as
/api/books/1
Instead of
/books/1
This is done in your routes
Route::get('/api/books/{id}', 'BookController@show');
You should read a little about API creation too, because API architecture is is hard, endpoints get messy really fast and easy, this is a nice book on APIs https://leanpub.com/build-apis-you-wont-hate
Upvotes: 5