Skip to content

Instantly share code, notes, and snippets.

@vishwac09
Created February 26, 2022 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vishwac09/eb655d9e766bceae6026b45c6efb0c36 to your computer and use it in GitHub Desktop.
Save vishwac09/eb655d9e766bceae6026b45c6efb0c36 to your computer and use it in GitHub Desktop.
RestResourceHandlers
<?php
// pseudo code
class VehicleFetchRestResource {
// GET /vehicle
public function get(Request $request) {
try {
// #1 Authentication layer
// Validate if the request is coming from trusted source.
Authenticate($request);
// #2 Authorisation layer
// Does the current user have permission to fetch the resource.
Authorise($request);
// Validate the request, applicable to GET/POST/PUT/PATCH.
$query = $request->query->getAll();
Validator::validate($query);
// Get the Request filters, applicable for GET requests.
// Filters have the following values sent from client.
// e.g. /vehicles?limit=10&page=1&featured=true
$filters = $this->getRequestFilters($query);
// Create a service/utility File to get the data.
$data = Service::fetchVehicles($filters);
// Massage the data if needed.
$data = Massage::alterData($data);
// Send the response.
return ResourceResponse(200, $data);
} catch (\Exception $e) {
throw new \Exception('Error', 400);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment