Closed kvvaradha closed 2 years ago
Hey, can you please explain that in more detail? As I know I don't have an working api example inside the code.
yes, i asked the same,
Can you provide working sample for the API. ?
hope its only for the routing, with authentication process and data return format here.
Ok. You want an API example. But which kind of API? JSON? REST? GraphQL?
So here is a simple endpoint that will return some users as JSON:
Route::add('/users', function() {
$users = array("a" => "Tom", "b" => "Thea", "c" => "Tim");
header("Content-Type: application/json");
echo json_encode($users);
});
How about doing it with REST API ?
The example above is already a part of the rest specification. You have to build all REST routes on your own. For example implement a delete route. Use it together with the request method validation:
Route::add('/users/([0-9]*)', function() {
// Delete the user in here
header("Content-Type: application/json");
echo json_encode(['status':'success']);
}, 'delete');
To complete this RESTfull list (Code untestet):
// Create a new user
Route::add('/users', function() {
// Insert post data to database
header("Content-Type: application/json");
echo json_encode(['status':'success']);
}, 'post');
// Return all users
Route::add('/users', function() {
// Read all users from database
$users = array("a" => "Tom", "b" => "Thea", "c" => "Tim");
header("Content-Type: application/json");
echo json_encode($users);
}, 'get');
// Bulk update all users
Route::add('/users', function() {
// Bulk replace all users with the new data from the request
header("Content-Type: application/json");
echo json_encode(['status':'success']);
}, 'put');
// Delete all users
Route::add('/users', function() {
// Delete all users from database
header("Content-Type: application/json");
echo json_encode(['status':'success']);
}, 'delete');
// Fetch a single user
Route::add('/users/([0-9]*)', function($userId) {
// Read the user from database
header("Content-Type: application/json");
echo json_encode(['status':'success']);
}, 'get');
// Update a single user
Route::add('/users/([0-9]*)', function($userId) {
// Update the user inside the database
header("Content-Type: application/json");
echo json_encode(['status':'success']);
}, 'put');
// Delete a single user
Route::add('/users/([0-9]*)', function($userId) {
// Delete the user from database
header("Content-Type: application/json");
echo json_encode(['status':'success']);
}, 'delete');
Please read this spec: https://wiki.onap.org/display/DW/RESTful+API+Design+Specification
First of all, This is absolutely a big functionality in simple code.