alexpensato/laravel-api-maker
Composer 安装命令:
composer require alexpensato/laravel-api-maker
包简介
Automate the generation of your REST APIs
README 文档
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/usersGET api/v1/users/{id}POST api/v1/usersPUT 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!
alexpensato/laravel-api-maker 适用场景与选型建议
alexpensato/laravel-api-maker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 436 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 08 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「generator」 「rest」 「api」 「BDD」 「repository」 「fractal」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 alexpensato/laravel-api-maker 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alexpensato/laravel-api-maker 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 alexpensato/laravel-api-maker 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Memio's PrettyPrinter, used to generate PHP code from given Model
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
A Flickr wrapper to allow you to call the Flickr api with Guzzle as the backend.Goal is to have 100% Flickr api coverage rather than just upload/display photos (currently at 23%).
Phalcon Model Generator
Auto-generate dummy data with realistic relationships per Model-like config classes
统计信息
- 总下载量: 436
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-08-29