team-moment / fractal
fractal for FastD framework
v1.0.6
2018-06-01 09:43 UTC
Requires
- php: ^7.1
- fastd/fastd: ~3.0
- illuminate/pagination: ^5.5
- league/fractal: ^0.17.0
Requires (Dev)
- phpunit/phpunit: ^5.0
This package is not auto-updated.
Last update: 2025-03-30 08:27:51 UTC
README
Usage
首先定义 Tranformer
<?php namespace Transformer; use Moment\Fractal\AbstractTransformer; class AppTransformer extends AbstractTransformer { public function transform($app) { return [ 'id' => $app->id, 'name' => $app->name, ]; } }
<?php namespace Controller; use Models\App; use FastD\Http\ServerRequest; use Transformer\AppTransformer; use FastD\Http\Response; class AppsController { public function show(ServerRequest $request) { $app = App::find($request->getAttribute('id')); return fractal()->item($app, AppTransformer::class, Response::HTTP_OK); } }
请求后, 将输出
HTTP 200 OK
{"id": 1, "name": "testing"}