nimaebrazi/laravel-api-presenter
Composer 安装命令:
composer require nimaebrazi/laravel-api-presenter
包简介
Provide client response with simple cache.
README 文档
README
Description
Simple and powerful client response integrated with Laravel cache.
Installation
composer require nimaebrazi/laravel-api-presenter
Register ServiceProvider
If using laravel 5.4.* and older version you need add service provider in config/app.php
'providers' => [ ... \LaravelApiPresenter\ApiPresenterServiceProvider::class, ... ]
Usage
1. Response without cache
Example 1.1
<?php namespace App\Http\Controllers; use App\User; use Illuminate\Http\Request; use LaravelApiPresenter\Contract\ApiPresenterInterface; use LaravelApiPresenter\Presenter\Model\ApiPresenterModel; class UserController extends Controller { /** * @var ApiPresenterInterface */ protected $apiPresenter; public function __construct(ApiPresenterInterface $apiPresenter) { $this->apiPresenter = $apiPresenter; } public function find($id) { $user = User::find($id); $apiPresenterModel = new ApiPresenterModel(); $apiPresenterModel ->withSuccessStatus() ->setMessage('Success fetch!') ->setMainKey('user') ->setData($user->toArray()); return $this->apiPresenter->present($apiPresenterModel); } }
{
"success": true,
"message": "Success fetch!",
"description": "",
"data": {
"main_key": "user",
"user": {
"id": 1,
"name": "Mr. Claude Greenfelder I",
"email": "carlie84@example.com",
"email_verified_at": "2019-03-19 00:07:52",
"created_at": "2019-03-19 00:07:52",
"updated_at": "2019-03-19 00:07:52"
}
}
}
Example 1.2
<?php namespace App\Http\Controllers; use App\User; use Illuminate\Http\Request; use LaravelApiPresenter\Contract\ApiPresenterInterface; use LaravelApiPresenter\Presenter\Model\ApiPresenterModel; class UserController extends Controller { /** * @var ApiPresenterInterface */ protected $apiPresenter; public function __construct(ApiPresenterInterface $apiPresenter) { $this->apiPresenter = $apiPresenter; } public function find() { $ids = [1,2,3]; $user = User::find($ids); $apiPresenterModel = new ApiPresenterModel(); $apiPresenterModel ->withSuccessStatus() ->setMessage('Success fetch!') ->setMainKey('user') ->setData($user->toArray()); return $this->apiPresenter->present($apiPresenterModel); } }
{
"success": true,
"message": "Success fetch!",
"description": "",
"data": {
"main_key": "user",
"user":
[
{
"id": 1,
"name": "Mr. Claude Greenfelder I",
"email": "carlie84@example.com",
"email_verified_at": "2019-03-19 00:07:52",
"created_at": "2019-03-19 00:07:52",
"updated_at": "2019-03-19 00:07:52"
},
{
"id": 2,
"name": "Josiane Rath IV",
"email": "isobel66@example.org",
"email_verified_at": "2019-03-19 00:07:52",
"created_at": "2019-03-19 00:07:52",
"updated_at": "2019-03-19 00:07:52"
},
{
"id": 3,
"name": "Mr. Godfrey Witting I",
"email": "karine35@example.org",
"email_verified_at": "2019-03-19 00:07:52",
"created_at": "2019-03-19 00:07:52",
"updated_at": "2019-03-19 00:07:52"
}
]
}
}
2. Response single object with cache
Example 2.1
<?php namespace App\Http\Controllers; use App\User; use Illuminate\Http\Request; use LaravelApiPresenter\Contract\ApiPresenterInterface; use LaravelApiPresenter\Presenter\Model\ApiPresenterModel; class UserController extends Controller { /** * @var ApiPresenterInterface */ protected $apiPresenter; public function __construct(ApiPresenterInterface $apiPresenter) { $this->apiPresenter = $apiPresenter; } public function find($id) { $user = User::find($id); $apiPresenterModel = new ApiPresenterModel(); $apiPresenterModel->withSuccessStatus() ->setMessage('Success fetch!') ->setMainKey('user') ->cacheable() ->setCacheKey("user_{$id}") ->setData($user->toArray()); return $this->apiPresenter->present($apiPresenterModel); } }
{
"success": true,
"message": "Success fetch!",
"description": "",
"data": {
"main_key": "user",
"user": {
"id": 1,
"name": "Mr. Claude Greenfelder I",
"email": "carlie84@example.com",
"email_verified_at": "2019-03-19 00:07:52",
"created_at": "2019-03-19 00:07:52",
"updated_at": "2019-03-19 00:07:52"
}
}
}
Note
It's depend on your application cache driver setting.
For example you can find output key if using redis like:
laravel_cache:user_1
3. Response with cache and auto pagination
Example 3.1
<?php namespace App\Http\Controllers; use App\User; use Illuminate\Http\Request; use LaravelApiPresenter\Contract\ApiPresenterInterface; use LaravelApiPresenter\Presenter\Model\ApiPresenterModel; class UserController extends Controller { /** * @var ApiPresenterInterface */ protected $apiPresenter; public function __construct(ApiPresenterInterface $apiPresenter) { $this->apiPresenter = $apiPresenter; } public function all(Request $request) { $limit = $request->has('limit') ? $request->input('limit') : 5; $users = User::paginate($limit); $apiPresenterModel = new ApiPresenterModel(); $apiPresenterModel ->withSuccessStatus() ->setMessage('Success fetch!') ->setMainKey('users') ->withMeta() ->cacheable() ->setCacheKey("users") ->setData($users->toArray()); return $this->apiPresenter->present($apiPresenterModel); } }
{
"success": true,
"message": "Success fetch!",
"description": "",
"data": {
"main_key": "users",
"users":
[
{
"id": 1,
"name": "Mr. Claude Greenfelder I",
"email": "carlie84@example.com",
"email_verified_at": "2019-03-19 00:07:52",
"created_at": "2019-03-19 00:07:52",
"updated_at": "2019-03-19 00:07:52"
},
{
"id": 2,
"name": "Josiane Rath IV",
"email": "isobel66@example.org",
"email_verified_at": "2019-03-19 00:07:52",
"created_at": "2019-03-19 00:07:52",
"updated_at": "2019-03-19 00:07:52"
},
{
"id": 3,
"name": "Mr. Godfrey Witting I",
"email": "karine35@example.org",
"email_verified_at": "2019-03-19 00:07:52",
"created_at": "2019-03-19 00:07:52",
"updated_at": "2019-03-19 00:07:52"
}
]
},
"links": {
"first": "http://127.0.0.1:8000/users?page=1",
"last": "http://127.0.0.1:8000/users?page=34",
"next": "http://127.0.0.1:8000/users?page=2",
"prev": null
},
"meta": {
"current_page": 1,
"from": 1,
"last_page": 34,
"path": "http://127.0.0.1:8000/users",
"per_page": "3",
"to": 3,
"total": 100
}
}
Note:
Package generate key from meta and transform cache key in redis like:
laravel_cache:users__limit_3__page_1
4. Response with cache and custom pagination
- Pay attention, when use custom meta, you should make custom cache key too.
Suppose you set cache key
usersand paginate your data and respond to clientpage one. So package set your cacheusersand not append meta data to cache key. And your response not correct when client limit the result of data and page. - Don't use
withMeta()method when set custom meta data.
Example 4.1
<?php namespace App\Http\Controllers; use App\User; use Illuminate\Http\Request; use LaravelApiPresenter\Contract\ApiPresenterInterface; use LaravelApiPresenter\Presenter\Model\ApiPresenterModel; use LaravelApiPresenter\Presenter\Model\MetaModel; class UserController extends Controller { /** * @var ApiPresenterInterface */ protected $apiPresenter; public function __construct(ApiPresenterInterface $apiPresenter) { $this->apiPresenter = $apiPresenter; } public function all(Request $request) { $limit = $request->has('limit') ? $request->input('limit') : 5; $users = User::paginate($limit); $metaModel = new MetaModel(); $metaModel->setCurrentPage(1) ->setLastPage(3) ->setTo(3) ->setTotal(50); $apiPresenterModel = new ApiPresenterModel(); $apiPresenterModel ->withSuccessStatus() ->setMessage('Success fetch!') ->setMainKey('users') ->withMeta() ->cacheable() ->setCacheKey("users__limit_1__page_1") ->setData($users->toArray()); return $this->apiPresenter->present($apiPresenterModel); } }
For more info about MetaModel read class:
LaravelApiPresenter\Presenter\Model\MetaModel
How to set custom response?
You access to response object in ApiPresenter.
Suppose you want change response header:
... $this->apiPresenter->response() ->header('X-Header-One', 'Header Value One ') ->header('X-Header-Two', 'Header Value Two'); ...
nimaebrazi/laravel-api-presenter 适用场景与选型建议
nimaebrazi/laravel-api-presenter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 11, 最近一次更新时间为 2019 年 03 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 nimaebrazi/laravel-api-presenter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nimaebrazi/laravel-api-presenter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-03-26