Skip to content

Laravel 5.1 RESTFul API to perform POST (and many more) requests and also handles one time token for users to make API calls.

Notifications You must be signed in to change notification settings

xSavitar/LaraAPI

Repository files navigation

Laravel RESTful API with JWT - Various API calls

  • /api/v1/register
  • /api/v2/login
  • /api/v1/me
  • /api/v1/addproduct

Source

Table of Contents

Installation

composer install
php artisan migrate:refresh

Folder Structure

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

API Debug

Enable more detailed output to your api. For enable set API_DEBUG=true

Default Routes

For prevent problems with responses, send a Accept: application/json header for every request

Authentication

POST api/v1/login

  • Request

    • email (string) - Your email
    • password (string) - Your password
  • Response 200 (application/json)

    • Body

        {
          "status_code": 200,
          "token": "your_token_here"
        }
      

POST api/v1/register

  • 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"
        }
      

GET api/v1/me (need jwt token)

  • 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"
            }
          }
        }
      

PUT api/v1/me (need jwt token)

  • Request

    • Headers

            Authorization: Bearer your_token_here
      
  • Response 200 (application/json)

    • Body

        {
          "status_code": 200,
          "message": "Profile Updated successfully"
        }
      

DELETE api/v1/me (need jwt token)

  • Request

    • Headers

            Authorization: Bearer your_token_here
      
  • Response 204 No Content (application/json)

    • Body

Transformers

Transformers allow you to output flexible data structures for your API, an example integrated with the User repository is available

Responses and Errors

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();

''

...

Validation Errors

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

Pagination

You can create a pagination manually or using a model or repository

About

Laravel 5.1 RESTFul API to perform POST (and many more) requests and also handles one time token for users to make API calls.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published