origami / api
Laravel 5 API 包
Requires
- php: ^7.2|^8.0.2
- illuminate/contracts: ^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/http: ^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/support: ^6.0|^7.0|^8.0|^9.0|^10.0
- league/fractal: ^0.19
- nesbot/carbon: ^2.0
README
此包简化了使用 Laravel 6 设置 API 的过程。此包中使用的许多实践得益于 Phil Sturgeon 的《Build APIs you Won't Hate》(https://leanpub.com/build-apis-you-wont-hate)。
安装
通过 Composer 安装此包。
composer require origami/api
需求
目前此包设计为与 Laravel >= 6 一起使用。
配置
有一些 API 配置选项你可能想要覆盖。首先,发布默认配置。
php artisan vendor:publish
这将在以下位置添加一个新的配置文件:config/api.php
。
<?php return array( 'keys' => [ env('API_KEY', 'secret') ], );
keys
这是有效的 API 密钥列表,用于验证请求。默认情况下,我们支持环境变量 API_KEY
,你可以在 .env 文件中设置它。
中间件
此包包含两个 Laravel 6 中间件类
Origami\Api\Middleware\AuthenticateApiKey
AuthenticateApiKey 中间件旨在保护 API 路由不受未经授权的访问。除非你有公共 API,否则我们建议你在所有路由上包含它,如下所示:
Route::group(['prefix' => 'api/v1', 'namespace' => 'Api', 'middleware' => 'Origami\Api\Middleware\AuthenticateApiKey'], function() { Route::get('tasks/{id}', 'Tasks@show'); Route::post('tasks/{id}', 'Tasks@update'); });
控制器
我们提供了一个有用的 ApiController 基础控制器类,它包含一个 response
方法,允许你返回 JSON 响应或访问提供各种辅助方法的 Origami\Api\Response 类。
响应
Origami/Api/Response 类提供各种辅助方法,并最终使用 Laravel 的 Illuminate\Contracts\Routing\ResponseFactory
类返回带有适当头部的 JSON 响应。
你可以在控制器中使用 API 响应类,通过使用 response
辅助方法
public function index() { $items = new Collection(['one','two','three']); // Calling with a single argument returns a json response return $this->response($items); }
或
public function index() { $items = new Collection(['one','two','three']); // Calling with no argument returns the response object return $this->response()->data($items); } public function find($id) { $item = Item::find($id); if ( ! $item ) { // Using the response object you can call helper methods. return $this->response()->errorNotFound(); } return $this->response()->data($item); }
项目或集合
我们使用优秀的 league/fractal PHP 包来解析和转换 Eloquent 模型和集合。更多信息请访问 fractal.thephpleague.com
响应对象有三个辅助方法
- resourceItem
- resourceCollection
- resourcePaginator
完整示例
路由:app/Http/routes.php
<?php Route::group(['prefix' => 'api/v1', 'namespace' => 'Api', 'middleware' => 'Origami\Api\Middleware\AuthenticateApiKey'], function() { Route::get('items', 'Items@index'); Route::get('tasks/{id}', 'Tasks@show'); });
控制器:app/Http/Controllers/Api/Items.php
<?php namespace App\Http\Controllers\Api; use Origami\Api\ApiController; use App\Items\Item; // Assuming an eloquent Model; use App\Items\ItemTransformer; class Items extends ApiController { public function index() { $items = Item::paginate(15); return $this->response()->resourcePaginator($items, new ItemTransformer); } public function show($id) { $item = Item::find($id); if ( ! $item ) { return $this->response()->errorNotFound('Item not found'); } return $this->response()->resourceItem($item, new ItemTransformer); } }
转换器:app/Items/ItemTransformer.php
<?php namespace App\Items; use Origami\Api\Transformer; class ItemTransformer extends Transformer { public function transform(Item $item) { return [ 'id' => $item->id, 'title' => $task->title, 'created_at' => $task->created_at->toDateTimeString() ]; } }