alexpensato / laravel-api-maker
Automate the generation of your REST APIs
Requires
- php: ^7.1.3
- ext-json: ^1.6
- codeception/specify: ^1.1
- codeception/verify: ^1.0
- guzzlehttp/guzzle: ^6.3
- illuminate/support: ^5.6
- league/fractal: ^0.17
Requires (Dev)
- phpunit/phpunit: ^7.0
This package is auto-updated.
Last update: 2025-02-28 03:20:29 UTC
README
Automate the generation of your REST APIs with the console generator command make:api
Introduction
This package is an extended version of the laravel-api-generator package. It includes a completely rewritten BaseController, now renamed to ApiController. This package also presents two new controller types: ReadOnlyController, for APIs that don't need writing capabilities; and WebController, for applications that need support for frontend scaffolding views.
This package relies on the Repository Design Pattern, which means that Controllers will access Models through a Repository interface, which provides better separation of concerns for data access methods and business rules. This repository implementation was inspired by Connor Leech's and Jeff Decena's articles.
This package also uses Codeception/Specify and Codeception/Verify packages to get you started with BDD-style unit testing.
This enhanced version of the console generator creates the following files for each Model in one single command:
-
ApiController extended class
-
Fractal Transformer class
-
Repository interface
-
Repository implementation class
-
Unit test file configured for BDD
It also modifies the following configuration files:
-
Adds routes to routes/api.php
-
Adds repository binding to ApiServiceProvider
This package was designed to get you started with professional REST API best practices.
Compatibility
Laravel API Maker | Laravel |
---|---|
1.0.x | 5.6 |
1.1.x | 5.7 |
Installation
Step 1 - Run composer require alexpensato/laravel-api-maker
Step 2 - Copy the ApiServiceProvider
class to app/Providers
folder:
cp -R vendor/alexpensato/laravel-api-maker/templates/Providers app/Providers
and check what you got there.
Step 3 - Register the service providers in the config/app.php
configuration file
<?php 'providers' => [ ... /* * Package Service Providers... */ Pensato\Api\ServiceProvider::class, /* * Application Service Providers... */ App\Providers\ApiServiceProvider::class, ], ?>
Step 4 - Laravel already provides an API routes file. To correctly configure the console generator automation process,
you need to choose one of the routing templates presented in vendor/alexpensato/laravel-api-maker/templates/routes/api.php
,
and then copy it to your project's api routing file.
For instance, copy the code below to the end of your routes/api.php
project file.
<?php Route::group(['prefix' => 'v1'], function () { // });
It will allow the console generator to automatically inject resources routes to the api routing file.
Usage
Generator
The only console command that is added and needed is artisan make:api <ModelName>
.
Imagine you need to create a rest api to list/create/update etc. users from users table. To achieve that you need to do lots of boilerplate operations - create controller, transformer, repository, unit testing, set up needed routes and configuring repository binding.
php artisan make:api User
does all the work for you.
It is important to notice that this command assumes that Model has already been created in the Models
folder.
For instance, you can create a Model using the following command:
php artisan make:model -mf Models/<ModelName>
Conventions
You may have noticed that the Controller
which has just been generated includes two public methods: __constructor()
and transformer()
.
That's because those methods are the only thing that you need in your controller to set up a basic REST API.
The list of routes that are available out-of-the-box:
GET api/v1/users
GET api/v1/users/{id}
POST api/v1/users
PUT api/v1/users/{id}
DELETE api/v1/users/{id}
Request and response formats are JSON. Response format is defined by Fractal.
Fractal includes are supported via $_GET['include']
.
Validation rules for create and update can be set by overwriting rulesForCreate
and rulesForUpdate
in your controller.
Contributions
…are always welcome. Don’t hesitate to submit a bug report or a pull request.
When filling a bug report or submitting a new feature, please try including supporting test cases.
License
This project is licensed under MIT license.
Although it is a tested package, use at your risk!