Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
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.
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.
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
Also Read: Standardise API responses in Laravel
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.
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
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.
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.
You can modify messages as per your requirements.
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.
This boilerplate utilizes a simple wrapper for maintaining Roles & Permission for your Laravel API.
You can create your custom endpoints in routes/api.php under auth:api middleware group.
This boilerplate API comes in with a CORS package to ease your worries. Just use the cors middleware on the required routes where necessary.
POST api/v1/loginPOST api/v1/registerPOST api/v1/recoverPOST api/v1/resetGET api/v1/reset/{token}GET api/v1/logoutapi/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
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.