steampixel / simplePHPRouter

This is a simple and small single class PHP router that can handel the whole url routing for your project.
MIT License
406 stars 116 forks source link

can you provide an additional example for the api example ? #57

Closed kvvaradha closed 2 years ago

kvvaradha commented 2 years ago

First of all, This is absolutely a big functionality in simple code.

steampixel commented 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.

kvvaradha commented 2 years ago

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.

steampixel commented 2 years ago

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);
});
kvvaradha commented 2 years ago

How about doing it with REST API ?

steampixel commented 2 years ago

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');
steampixel commented 2 years ago

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');
steampixel commented 2 years ago

Please read this spec: https://wiki.onap.org/display/DW/RESTful+API+Design+Specification