Building RESTful API
Based on Laravel PHP MVC Framework
2020/2021(2013) AC
Contents
Basics of RESTful API
Restful API using Laravel framework
RESTful API Resources
> Model
> Controller
Endpoint of API – URL/Route
RESTful API Model and Eloquent
Using API tools – Postman, Fiddler,…
Consume RESTful API
Selected Topics in Computer Science 2013(2020/21)AY 2
RESTful API
REST
> REpresentable State Transfer
Uses standard HTTP
> Can use any text format including XML, JSON
Selected Topics in Computer Science 2013(2020/21)AY 3
RESTful API
Characteristics of a RESTful Service:
> Can be called to retrieve business information from the server
> Can create, update, and delete information in a database through HTTP operations
> Uses URLs to uniquely identify the entity that it operates on
> Uses HTTP verbs to identify the operation that the application needs to perform.
> The HTTP verbs include:
GET
POST
PUT
DELETE
Selected Topics in Computer Science 2013(2020/21)AY 4
RESTful API based on Laravel Framework
Rest API – return a simple text representation such as
>JSON
>XML
So, create Api Controller method that return JSON,…
User file [Link] instead of [Link] file to define api
route
The Model and Database is the same as MVC
implementation
Selected Topics in Computer Science 2013(2020/21)AY 5
Building RESTfull API
Create API Controller
>Use command
php artisan make:controller CollegeApiController
>Or
You may consider
Creating apiResource controller
Selected Topics in Computer Science 2013(2020/21)AY 6
Rest API …
Define api route in api,php file
>Get all colleges: `GET /api/college/list`
>Get a single college: `GET /api/college/list/{id}`
>Create a new college: `POST /api/college/register`
>Update a college: `PUT /api/college/update
>Delete a college: `DELETE /api/college/delete/{id}`
Selected Topics in Computer Science 2013(2020/21)AY 7
Rest API …
Api routes
Route::get('/college/list', [CollegeApiController::class, 'index']);
Route::get('/college/list/{id}', [CollegeApiController::class, 'find']);
Route::post('/college/register', [CollegeApiController::class, 'store']);
Route::put('/college/update', [CollegeApiController::class, 'update']);
Route::delete('/college/delete/{id}', [CollegeApiController::class,
'destroy']);
Selected Topics in Computer Science 2013(2020/21)AY 8
Rest API …
Implement index() method in CollegeApiController class
class CollegeApiController extends Controller
{
public function index()
{
return College::all(); //default parsed to JSON
}
}
Selected Topics in Computer Science 2013(2020/21)AY 9
Rest API …
Run
>php artisan serve
>Browse to
[Link]
>You will get list of JSON as output
Selected Topics in Computer Science 2013(2020/21)AY 10
Rest API …
Implement find by id method
public function find($id)
{
return College::where('id', $id)->first();
}
Selected Topics in Computer Science 2013(2020/21)AY 11
Rest Api …
Register method
public function store(Request $request)
{
//Validate
$request->validate([
'name' => 'required|unique:colleges'
]);
$college = new College();
$college->name = $request->name;
$college->phone = $request->phone;
$success = $college->save();
return [
'success' => $success
];
}
Selected Topics in Computer Science 2013(2020/21)AY 12
Rest Api …
Update method
public function update(Request $request)
{
//Validate
$request->validate([
'name' => 'required',
'id' => 'required'
]);
$college = College::find($request->id);
$college->name = $request->name;
$college->phone = $request->phone;
$success = $college->save();
return [
'success' => $success
];
}
Selected Topics in Computer Science 2013(2020/21)AY 13
Rest Api …
Delete method
public function destroy($id)
{
$success = College::where('id', $id)->delete();
return [
'success' => $success
];
}
Selected Topics in Computer Science 2013(2020/21)AY 14
RESTful API Testing tools
RESTful API do not have user interface
>Developer need testing tool
RESTful API Testing tools include
>Postman
>Fiddler
>Browser plugging
Selected Topics in Computer Science 2013(2020/21)AY 15
Postman
Postman is a collaboration platform for API
development.
Postman's features simplify each step of building an
API and streamline collaboration so you can create
better APIs—faster.
Get postman from
[Link]
Selected Topics in Computer Science 2013(2020/21)AY 16
Postman …
User Interface
Selected Topics in Computer Science 2013(2020/21)AY 17
Postman …
Test Get all colleges: `GET /api/college/list`
> Run
php artisan serve
Get url [Link]
Go back to postman
Selected Topics in Computer Science 2013(2020/21)AY 18
Postman …
Test Get single college: `GET /api/college/list/2`
Get url [Link]
Go back to postman
Selected Topics in Computer Science 2013(2020/21)AY 19
Postman …
Test Register college: `POST /api/college/register`
Get url [Link]
Go back to postman
Selected Topics in Computer Science 2013(2020/21)AY 20
Postman …
Test Update college: `PUT /api/college/update`
Get url [Link]
Go back to postman
Selected Topics in Computer Science 2013(2020/21)AY 21
Postman …
Test DELETE college: `DELETE /api/college/update`
Get url [Link]
Go back to postman
Selected Topics in Computer Science 2013(2020/21)AY 22
Assignment – Build RESTful API
Work in group
Develop RESTful API based on Laravel framework
(at least one model with complete CRUD operation is
required)
Selected Topics in Computer Science 2013(2020/21)AY 23
Questions?
Selected Topics in Computer Science 2013(2020/21)AY 24