Skip to content

Instantly share code, notes, and snippets.

@GRAgmLauncher
Last active August 29, 2015 14:19
Show Gist options
  • Save GRAgmLauncher/7a4957d923fecfcc107f to your computer and use it in GitHub Desktop.
Save GRAgmLauncher/7a4957d923fecfcc107f to your computer and use it in GitHub Desktop.
API request validator concept
<?php
class APIRequest extends FormRequest
{
public function response(array $errors)
{
if ($this->ajax() || $this->wantsJson())
{
return new JsonResponse($errors, $this->responseCode());
}
}
protected function responseCode()
{
return 422;
}
}
<?php
class SomeEndpointRequest extends APIRequest
{
protected function responseCode()
{
return 417;
}
public function rules()
{
...
}
public function messages()
{
...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment