- /api/v1/register
- /api/v2/login
- /api/v1/me
- /api/v1/addproduct
- Fork/Built from https://github.com/mauri870/api-skeleton-laravel repo with additional feature and changed approach to POST, GET etc.. of the API.
composer install
php artisan migrate:refresh
The app
folder contains 3 subfolders.
Applications - Contains the applications served, like API's, Sites, etc
Core - Contains the core, all the features can be used in your applications
Domains - The domains available, like repositories and models
Feel free to edit and customize this structure
Enable more detailed output to your api. For enable set API_DEBUG=true
For prevent problems with responses, send a Accept: application/json
header for every request
-
Request
- email (string) - Your email
- password (string) - Your password
-
Response 200 (application/json)
-
Body
{ "status_code": 200, "token": "your_token_here" }
-
-
Request
- name (string) - Your name
- email (string) - Your email
- password (string) - Your password
- password_confirmation (string) - The same as your password
-
Response 200 (application/json)
-
Body
{ "status_code": 200, "token": "your_token_here" }
-
-
Request
-
Headers
Authorization: Bearer your_token_here
-
-
Response 200 (application/json)
-
Body
{ "status_code": 200, "data": { "type": "users", "id": "1", "attributes": { "name": "Test", "email": "test@test.com" } } }
-
-
Request
-
Headers
Authorization: Bearer your_token_here
-
-
Response 200 (application/json)
-
Body
{ "status_code": 200, "message": "Profile Updated successfully" }
-
-
Request
-
Headers
Authorization: Bearer your_token_here
-
-
Response 204 No Content (application/json)
- Body
Transformers allow you to output flexible data structures for your API, an example integrated with the User repository is available
All exceptions thrown within the scope of your api (see isApiCall
method in RestTrait) will be parsed by the laravel handler.
The Api exceptions will be parsed as json, the exceptions out of the api scope will be parsed with whoops
(if app is not in production and APP_DEBUG is true)
or otherwise by the normal handler.
Use the ResponseHelpers
trait for standardize your api responses:
$this->ApiResponse($data, $status_code);
$this->badRequest();
$this->modelNotFound();
$this->notFound();
$this->okButNoContent();
$this->methodNotAllowed();
$this->downForMaintenance();
Basically all the methods above are shortcuts and have the same signature as ApiResponse
, and they are also used by the rest handler too
If you can customize the pattern for all responses, edit the ApiResponse
method in App\Core\Traits\Rest\ResponseHelpers class
Remember to always use the methods from
ResponseHelpers
, because your project will always follow this pattern, so the maintenece is more easy
The default format for json responses is the following:
$this->ApiResponse('String Response');
{
"status_code" => 200,
"message" => 'String Response'
}
$this->ApiResponse(['joke' => "I'm a teapot"], 418);
{
"status_code" => 418,
"joke" => "I'm a teapot"
}
$this->badRequest();
{
"status_code" => 400,
"message" => "Bad Request"
}
$this->okButNoContent();
''
...
Please extend the App\Core\Http\Requests\Request
for your validators, because the json response has a little change comparing with default FormRequest
class.
In case of request is ajax
, wantsJson
or isApiCall
a json response with errors will be automatically generated with 422
status.
The response body for errors is someting like this:
{
"status_code": 422,
"message": "Validation failed",
"errors": {
"field": [
"Validation error is here"
],
"other_field": [
"Other validation error"
]
}
}
You can customize the default error message editing the response method in App\Core\Http\Requests\Request
You can create a pagination manually or using a model or repository