It will mapping all the requests to Controller, formatted and received all the parameters. That is exactly what the framework should do for us. It is quite similar to other frameworks in java/scala/python.
3. Controller
The actually controller is app/Http/Controllers/BookController.php
<?php
namespace App\Http\Controllers;
use App\Book;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class BookController extends Controller{
public function index(){
$Books = Book::all();
return response()->json($Books);
}
public function getBook($id){
$Book = Book::find($id);
return response()->json($Book);
}
public function createBook(Request $request){
$Book = Book::create($request->all());
return response()->json($Book);
}
public function deleteBook($id){
$Book = Book::find($id);
$Book->delete();
return response()->json('deleted');
}
public function updateBook(Request $request, $id){
$Book = Book::find($id);
That is just basic implementation, we may need validation and other logic there. The good things is that there is the simple ORM in this framework as well. I did not take care of any DAO logic now.
Command to start our service
> php artisan serve
Then, we can directly use POST man to test all our things.
I do not know how long this shared link will work, I will just paste it here.
https://www.getpostman.com/collections/e3273823c70a058eedfe