Get started with building APIs with Laravel

Starting off with building an API? Laravel API Boilerplate is a starting point for your APIs which helps you keeps going with structuring and developing your API faster than ever..

In this tutorial, we’ll be using an open-source library for building our next API with Laravel.

Who’s this for?

Developers who create and develop APIs on daily basis and have to repeat and go through several steps for setting up things every single time. This boilerplate will definitely cut them some slack and help them to keep going with the development like breeze.

Create new Laravel API

For this short tutorial, we’ll be using a simple boilerplate for developing Laravel API.

This package is build on top of the following

Create a new Laravel API boilerplate by using the following command in your projects directory

composer create-project usamamuneerchaudhary/laravel-api-boilerplate twt-api

It will create and setup a simple boilerplate app twt-api for you pre-installed with some basic dependencies and codebase required to keep you going.

Setting up the project is easier, just follow along with the README.md file.

Dig In

Now let’s head over to our project directory and see what we got here.

This API boilerplate comes in with the following pre-built setup

  • Laravel Passport for Authentication
  • Custom Roles & Permissions wrapper
  • CORS Middleware
  • CRUD Scaffolding
  • Ready to use Auth Controllers
  • Ready to use Custom Validations
  • Ready to use Custom Auth Notifications
  • Custom Firewall
  • Complete set of Auth Endpoints
  • and many more.

Directory Structures

All of your controllers, notifications, requests are namespaced under API followed by the module folder. For instance, in case of Auth, all files must be inside of API\Auth directory. It’s totally upto you if you want to follow this convention or not.

Notifications

Auth notifications have been setup already to reduce more hassle for you. You can find these under App\Notifications\API\Auth namespace. Currently there’re following notifications for Auth module.

  • User Registration
  • Password Reset

You can modify messages as per your requirements.

CRUD Scaffolding

A simple CRUD Scaffolding package comes built-in with this boilerplate. It saves you tons of time in creating files. This package comes with a custom php artisan commands for creating different resources.

Make sure to publish the library’s configuration

php artisan vendor:publish --tag=laravel-crud-scaffold

For instance if you wish to scaffold a Model, you can do something like

php artisan mbt:scaffold Model

This command will generate the following files for you like a breeze.

Roles & Permissions

This boilerplate utilizes a simple wrapper for maintaining Roles & Permission for your Laravel API.

Read more about setting up Roles & Permissions in Laravel

Creating Endpoints

You can create your custom endpoints in routes/api.php under auth:api middleware group.

Handling CORS

This boilerplate API comes in with a CORS package to ease your worries. Just use the cors middleware on the required routes where necessary.

Default Endpoints

  • Login: POST api/v1/login
  • Register: POST api/v1/register
  • Recover POST api/v1/recover
  • Reset POST api/v1/reset
  • Find Reset Token GET api/v1/reset/{token}
  • Logout GET api/v1/logout
  • Verify Email api/v1/user/verify/email/{token}

You can change the API route prefix in RouteServiceProvider.php if required. Currently it’s been set to api/v1

Start Building APIs

Well that’s it for this tut, you can start building your next-gen API using this boilerplate. Also, if you’ve any feedbacks or comments for improvements, you’re always welcome to share your thoughts in the comment section.

Github Repo

Comments