juliomotol/lapiv
Composer 安装命令:
composer require juliomotol/lapiv
包简介
API versioning for Laravel made easy
关键字:
README 文档
README
This package is feature locked
With the recent developments in PHP, Laravel and its community of packages, it has been easier than ever to manage routes for versioned APIs.
We have found better ways to do what this package does in a much cleaner way. We suggest for you to take a look at spatie/laravel-route-attributes or spatie/laravel-route-discovery.
With that, we will still continue to provide support for future PHP/Laravel updates until any major breakage.
A Small Laravel package for a simple and easy API versioning.
Lapiv simply stands for (L)aravel (API) (V)ersioning.
Installation
You can install the package via composer:
composer require juliomotol/lapiv
Config
| Key | Default Value | Description |
|---|---|---|
| default | "uri" |
The versioning method. Supports: "uri", "query_string". |
| methods.uri.prefix | "v{version}" |
The prefix for uri based versioning. (NOTE: Always include the "version" parameter in the prefix) |
| methods.query_string.key | "v" |
The query string key name for determining the version |
If you want to make changes in the configuration you can publish the config file using:
php artisan vendor:publish --provider="JulioMotol\Lapiv\LapivServiceProvider"
Setup
Now the juicy part, we'll walk you through how to setup versioned Controllers.
FooV1Controller.php
This is very much like your standard controller. Nothing special here really. All action methods must be declared here e.g. index, create, show, etc.
namespace App\Http\Controllers\Api\Foo; use App\Http\Controllers\Controller; class FooV1Controller extends Controller { public function index() { return response()->json(['message' => 'This is Foo V1']); } }
FooGatewayController.php
Now the good stuff. This controller MUST extend \JulioMotol\Lapiv\GatewayController in order for this whole thing to work. This will be in charge of dispatching the requests based on the requested version. Let's take a look inside.
namespace App\Http\Controllers\Api\Foo; use JulioMotol\Lapiv\GatewayController; class FooGatewayController extends GatewayController { protected array $apiControllers = [ FooV1Controller::class, // The first version of you API endpoint. // Preceeding version implementations... ]; }
The order in
$apiControllersis critical. The first controller declared will be ourv1, then will bev2, and so on.
Routing
With our controllers ready to go, lets create our route. Go to routes/api.php.
/** * Registers a versioned API endpoint. * * Router::lapiv($callback = null) * * @param $callback */ Route::lapiv(function () { Route::get('/foo', [FooGatewayController::class, 'index']); Route::get('/bar', [BarGatewayController::class, 'index']); });
Notice we didn't point to the [FooV1Controller::class, 'index']. As we've said, the FooGatewayController will be doing much of the heavy work, so we'll just call that instead.
When you run php artisan route:list you should see this.
| Method | URI | Action |
|---|---|---|
| GET|HEAD | api/v{version}/foo | App\Http\Controllers\Api\FooGatewayController@index |
| GET|HEAD | api/v{version}/bar | App\Http\Controllers\Api\BarGatewayController@index |
Now, when we try to go to /api/v1/foo, it should be handled by FooV1Controller.
Bumping a version
When your ready to bump your API version to v2, Simply add a new FooV2Controller and dont forget to add that to FooGatewayController's $apiControllers.
Versioning Methods
This package supports 2 types of API Versioning methods, uri and query_string.
uri Method
This is the default of the versioning method. Here, the API version will be declared in the uri path (e.g. example.com/api/v1/foo).
In the config, you can change the prefix for the uri.
"methods" => [ "uri" => [ "prefix" => '/version-{version}' // will generate `example.com/api/version-1/foo` ] ]
Don't forget to add the
versionparameter in the prefix.
query_string Method
Here, the API version will be declared in the query string (e.g. example.com/api/foo?v=1).
In the config, you can change the query string key.
"methods" => [ "query_string" => [ "key" => 'version' // will accept `example.com/api/foo?version=1` ] ]
Want to handle your own versioning method?
You can define how you want to handle versioning your APIs by extending JulioMotol\Lapiv\Drivers\BaseDriver:
use JulioMotol\Lapiv\Drivers\BaseDriver; use Illuminate\Support\Facades\Request; class HeaderDriver extends BaseDriver { public function getVersion(): string|int { $headerValue = Request::header($methodOptions['key']) ?? null; $matches = []; preg_match('/application\/vnd\.my_application\.v(\d*)\+json/', $headerValue, $matches); return $matches[1] ?? null; } }
You can also handle routing by overriding the
routeGroup()method in theBaseDriver.
Then add your new API driver in your AppServiceProvider::boot():
class AppServiceProvider extends ServiceProvider { public function boot() { ApiVersioningManager::extend('header', fn () => new HeaderDriver()); } }
And finally, use your new driver in the config/lapiv.php
'default' => 'header', // the value here will be the first parameter you've set in ApiVersioningManager::extend()
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email julio.motol89@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.
juliomotol/lapiv 适用场景与选型建议
juliomotol/lapiv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.2k 次下载、GitHub Stars 达 23, 最近一次更新时间为 2020 年 09 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「versioning」 「juliomotol」 「lapiv」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 juliomotol/lapiv 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 juliomotol/lapiv 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 juliomotol/lapiv 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Prompt users to re-enter their password before performing sensitive actions.
Versioning behaviour for eloquent models
A decent, standards-compliant, Semantic Versioning (SemVer) parser and library
A Laravel package for managing model drafts.
A PSR-7 compatible library for making CRUD API endpoints
Initiate versioning the database like code.
统计信息
- 总下载量: 8.2k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 23
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-09-17