runner/fastd-fractal

This package is abandoned and no longer maintained. No replacement package was suggested.

fractal for FastD framework

v2.0.2 2018-06-14 08:17 UTC

This package is auto-updated.

Last update: 2020-01-28 13:17:52 UTC


README

Usage

首先定义 Tranformer

<?php
namespace Transformer;

use Runner\FastdFractal\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"}