• Stars
    star
    1,174
  • Rank 39,823 (Top 0.8 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 9 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Laravel 5.8 API Boilerplate to create a ready-to-use REST API in seconds.

Laravel API Boilerplate (JWT Edition) for Laravel 5.8

Build Status

Laravel API Boilerplate is a "starter kit" you can use to build your first API in seconds. As you can easily imagine, it is built on top of the awesome Laravel Framework. This version is built on Laravel 5.8!

It is built on top of three big guys:

What I made is really simple: an integration of these three packages and a setup of some authentication and credentials recovery methods.

Installation

  1. run composer create-project francescomalatesta/laravel-api-boilerplate-jwt myNextProject;
  2. have a coffee, nothing to do here;

Once the project creation procedure will be completed, run the php artisan migrate command to install the required tables.

Usage

I wrote a couple of articles on this project that explain how to write an entire sample application with this boilerplate. They cover the older version of this boilerplate, but all the concepts are the same. You can find them on Sitepoint:

Just be aware that some options in the config/boilerplate.php file are changed, so take a look to it.

WARNING: the articles are old and Laravel 5.1 related. Just use them as "inspiration". Even without updated tutorials, they should be enough.

Main Features

Ready-To-Use Authentication Controllers

You don't have to worry about authentication and password recovery anymore. I created four controllers you can find in the App\Api\V1\Controllers for those operations.

For each controller there's an already setup route in routes/api.php file:

  • POST api/auth/login, to do the login and get your access token;
  • POST api/auth/refresh, to refresh an existent access token by getting a new one;
  • POST api/auth/signup, to create a new user into your application;
  • POST api/auth/recovery, to recover your credentials;
  • POST api/auth/reset, to reset your password after the recovery;
  • POST api/auth/logout, to log out the user by invalidating the passed token;
  • GET api/auth/me, to get current user data;

Separate File for Routes

All the API routes can be found in the routes/api.php file. This also follow the Laravel 5.5 convention.

Secrets Generation

Every time you create a new project starting from this repository, the php artisan jwt:generate command will be executed.

Configuration

You can find all the boilerplate specific settings in the config/boilerplate.php config file.

<?php

return [

    // these options are related to the sign-up procedure
    'sign_up' => [
        
        // this option must be set to true if you want to release a token
        // when your user successfully terminates the sign-in procedure
        'release_token' => env('SIGN_UP_RELEASE_TOKEN', false),
        
        // here you can specify some validation rules for your sign-in request
        'validation_rules' => [
            'name' => 'required',
            'email' => 'required|email',
            'password' => 'required'
        ]
    ],

    // these options are related to the login procedure
    'login' => [
        
        // here you can specify some validation rules for your login request
        'validation_rules' => [
            'email' => 'required|email',
            'password' => 'required'
        ]
    ],

    // these options are related to the password recovery procedure
    'forgot_password' => [
        
        // here you can specify some validation rules for your password recovery procedure
        'validation_rules' => [
            'email' => 'required|email'
        ]
    ],

    // these options are related to the password recovery procedure
    'reset_password' => [
        
        // this option must be set to true if you want to release a token
        // when your user successfully terminates the password reset procedure
        'release_token' => env('PASSWORD_RESET_RELEASE_TOKEN', false),
        
        // here you can specify some validation rules for your password recovery procedure
        'validation_rules' => [
            'token' => 'required',
            'email' => 'required|email',
            'password' => 'required|confirmed'
        ]
    ]

];

As I already said before, this boilerplate is based on dingo/api and tymondesigns/jwt-auth packages. So, you can find many informations about configuration here and here.

However, there are some extra options that I placed in a config/boilerplate.php file:

  • sign_up.release_token: set it to true if you want your app release the token right after the sign up process;
  • reset_password.release_token: set it to true if you want your app release the token right after the password reset process;

There are also the validation rules for every action (login, sign up, recovery and reset). Feel free to customize it for your needs.

Creating Endpoints

You can create endpoints in the same way you could to with using the single dingo/api package. You can read its documentation for details. After all, that's just a boilerplate! :)

However, I added some example routes to the routes/api.php file to give you immediately an idea.

Cross Origin Resource Sharing

If you want to enable CORS for a specific route or routes group, you just have to use the cors middleware on them.

Thanks to the barryvdh/laravel-cors package, you can handle CORS easily. Just check the docs at this page for more info.

Tests

If you want to contribute to this project, feel free to do it and open a PR. However, make sure you have tests for what you implement.

In order to run tests:

  • be sure to have the PDO sqlite extension installed in your environment;
  • run php vendor/bin/phpunit;

Feedback

I currently made this project for personal purposes. I decided to share it here to help anyone with the same needs. If you have any feedback to improve it, feel free to make a suggestion, or open a PR!

More Repositories

1

laravel-feature

A package to manage feature flagging in a Laravel project.
PHP
209
star
2

weekend-learning

A list of things that you could learn in a weekend to improve your skills. Every suggestion is welcome!
65
star
3

laravel-reactions

Implement reactions for your Eloquent models, easily.
PHP
58
star
4

laraprep

A simple Laravel project preparation script, for a lazy developer.
Shell
34
star
5

laravel-api-boilerplate-angular-example

Example project for Laravel API Boilerplate JWT Edition
PHP
31
star
6

laravel-circuit-breaker

An implementation of the circuit breaker pattern for Laravel 5.6
PHP
28
star
7

awesome-stellar

A curated list of Stellar services and tools for software developers
23
star
8

costanzo

Tutte ttrontate.
PHP
22
star
9

laravel-amplitude

A Laravel package to work with Amplitude.
PHP
12
star
10

php-google-tts-example

A basic example script to use Google Cloud Text-to-Speech APIs
PHP
8
star
11

seeder

a php flexible and easy-to-use random data generator
PHP
5
star
12

hostr

A simple PHP CLI tool to manage your hosts file.
PHP
4
star
13

meanvm

A Simple Vagrant MEAN Virtual Machine
Shell
4
star
14

httpstatus

A simple http status code generator.
HTML
4
star
15

slacky

An easy-to-install, Lumen based invitation tool for your Slack team.
PHP
4
star
16

laravel-ecs-boilerplate

A starter boilerplate to deploy a Laravel application on AWS ECS + Fargate
Shell
4
star
17

wolframalphaphp

A PHP Wrapper for Wolfram|Alpha APIs (v2).
PHP
4
star
18

quo-bot

My first Facebook Messenger Bot, with Lumen
PHP
2
star
19

rsz

My first project with Go. A simple CLI image resizer.
Go
1
star
20

symfony-dockerized

This is a test, don't use this repo now.
Nginx
1
star
21

webdevbot

A bot for the Web Developer Italiani Facebook group.
PHP
1
star
22

loggo

A very basic logging service with a TCP server implementation to handle requests.
Go
1
star
23

laravel-antd-boilerplate

A boilerplate for Laravel 7 apps with React and Ant Design support
PHP
1
star