Skip to content

Instantly share code, notes, and snippets.

@vishwac09
Created February 25, 2022 10:15
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/daed95b6ab181a2370e62d74ca5759ef to your computer and use it in GitHub Desktop.
Save vishwac09/daed95b6ab181a2370e62d74ca5759ef to your computer and use it in GitHub Desktop.
RestfulValidationNotSoBad
<?php
class VehicleCreateRestResource {
// POST handler
public function post() {
try {
$payload = (array)json_decode($this->request->getContent());
$query = $this->request->query->all();
// Vehicle Validator pseudo code.
VehicleValidator::validate($payload);
// Proceed to Entity creation if all validation pass.
Vehicle::create($payload);
return new ModifiedResourceResponse([
"message" => 'Create a new Vehicle entity',
"code" => 200
], 200);
} catch (\Exception $e) {
$error = [
'error' => $e->getMessage(),
'code' => $e->getCode()
];
return new ModifiedResourceResponse($error, $e->getCode());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment