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
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.
All of your
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.
- User Registration
- Password Reset
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
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.
You can create your custom endpoints in
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.
- Find Reset Token
- Verify Email
You can change the
API route prefix in
RouteServiceProvider.php if required. Currently it’s been set to
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.