hesto / fractalfy
Fractal 的 Laravel 封装
v1.0.2
2017-02-13 10:08 UTC
Requires
- php: >=5.5.9
- illuminate/support: ~5.1.0|~5.2.0|~5.3.0|~5.4.0
- league/fractal: ^0.14.0
Requires (Dev)
- illuminate/pagination: ^5.3
This package is not auto-updated.
Last update: 2024-09-14 19:53:19 UTC
README
Fractal 的 Laravel 封装
使用方法
步骤 1: 通过 Composer 安装
composer require hesto/fractalfy
步骤 2: 注册服务提供者
将你的新提供者添加到 config/app.php
文件的 providers
数组中
'providers' => [ // ... Hesto\Fractalfy\FractalfyServiceProvider::class, // ... ],
Fractal 方法
使用 FractalfyController 扩展你的控制器
class DashboardController extends FractalfyController { ... }
返回集合
$users = Users::all(); return $this->fractal ->collection($users, new UserTransformer) ->get();
返回带分页的资源
$users = Users::all(); return $this->fractal ->paginate($users, new UserTransformer) ->get();
Fractalfy 辅助函数
使用 Fractalfy 辅助函数(已包含在 FractalfyController 中)
热门
return $this->respondOK(); return $this->respondNotFound(); return $this->respondUnauthorized(); return $this->respondUnprocessable(); return $this->respondBadRequest(); return $this->respondWithSuccess(200); //any success code return $this->respondWithError(400); //any success code
其他
return $this->respondOK($message); //pass message to respond return $this->setMessage($message)->respondOK(); return $this->setMessage($message)->setStatusCode($statuscode)->respondWithSuccess(); return $this->setMessage($message)->setStatusCode($statuscode)->respondWithError();