开源软件名称(OpenSource Name):spatie/laravel-fractal开源软件地址(OpenSource Url):https://github.com/spatie/laravel-fractal开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):An easy to use Fractal wrapper built for Laravel and Lumen applicationsThe package provides a nice and easy wrapper around Fractal for use in your Laravel applications. If you don't know what Fractal does, take a peek at their intro. Shortly said, Fractal is very useful to transform data before using it in an API. Using Fractal data can be transformed like this: use League\Fractal\Manager;
use League\Fractal\Resource\Collection;
$books = [
['id' => 1, 'title' => 'Hogfather', 'characters' => [...]],
['id' => 2, 'title' => 'Game Of Kill Everyone', 'characters' => [...]]
];
$manager = new Manager();
$resource = new Collection($books, new BookTransformer());
$manager->parseIncludes('characters');
$manager->createData($resource)->toArray(); This package makes that process a tad easier: fractal()
->collection($books)
->transformWith(new BookTransformer())
->includeCharacters()
->toArray(); Lovers of facades will be glad to know that a facade is provided: Fractal::collection($books)->transformWith(new BookTransformer())->toArray(); There's also a very short syntax available to quickly transform data: fractal($books, new BookTransformer())->toArray(); You can transform directly from a Laravel collection as well: collect($books)->transformWith(new BookTransformer()); Transforming right from a Laravel collection is particularly useful for Eloquent results: Users::all()->transformWith(new UserTransformer())->toArray(); Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website. Support usWe invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products. We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall. Installation in Laravel 5.5 and upYou can pull in the package via composer: composer require spatie/laravel-fractal The package will automatically register itself. If you want to change the default serializer,
the default paginator,
or the default fractal class php artisan vendor:publish --provider="Spatie\Fractal\FractalServiceProvider"
This is the contents of the published file: return [
/*
* The default serializer to be used when performing a transformation. It
* may be left empty to use Fractal's default one. This can either be a
* string or a League\Fractal\Serializer\SerializerAbstract subclass.
*/
'default_serializer' => '',
/* The default paginator to be used when performing a transformation. It
* may be left empty to use Fractal's default one. This can either be a
* string or a League\Fractal\Paginator\PaginatorInterface subclass.*/
'default_paginator' => '',
/*
* League\Fractal\Serializer\JsonApiSerializer will use this value to
* as a prefix for generated links. Set to `null` to disable this.
*/
'base_url' => null,
/*
* If you wish to override or extend the default Spatie\Fractal\Fractal
* instance provide the name of the class you want to use.
*/
'fractal_class' => Spatie\Fractal\Fractal::class,
'auto_includes' => [
/*
* If enabled Fractal will automatically add the includes who's
* names are present in the `include` request parameter.
*/
'enabled' => true,
/*
* The name of key in the request to where we should look for the includes to include.
*/
'request_key' => 'include',
], UsageRefer to the documentation of In all code examples you may use Send a response with transformed dataTo return a response with json data you can do this in a Laravel app. $books = fractal($books, new BookTransformer())->toArray();
return response()->json($books); The return fractal($books, new BookTransformer())->respond(); You can pass a response code as the first parameter and optionally some headers as the second return fractal($books, new BookTransformer())->respond(403, [
'a-header' => 'a value',
'another-header' => 'another value',
]); You can pass json encoding options as the third parameter: return fractal($books, new BookTransformer())->respond(200, [], JSON_PRETTY_PRINT); You can also set the status code and the headers using a callback: use Illuminate\Http\JsonResponse;
return fractal($books, new BookTransformer())->respond(function(JsonResponse $response) {
$response
->setStatusCode(403)
->header('a-header', 'a value')
->withHeaders([
'another-header' => 'another value',
'yet-another-header' => 'yet another value',
]);
}); You can add methods to the Fractal class using Laravel's Macroable trait. Imagine you want to add some stats to the metadata of your request, you can do so without cluttering your code: use Spatie\Fractal\Fractal;
Fractal::macro('stats', function ($stats) {
// transform the passed stats as necessary here
return $this->addMeta(['stats' => $stats]);
});
fractal($books, new BookTransformer())->stats(['runtime' => 100])->respond(); Quickly creating a transformerYou can run the UpgradingFrom v4 to v5Rename your config file from From v2 to v3
From v1 to v2In most cases you can just upgrade to
The main reason why ChangelogPlease see CHANGELOG for more information what has changed recently. Testing$ composer test ContributingPlease see CONTRIBUTING for details. SecurityIf you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker. CreditsLicenseThe MIT License (MIT). Please see License File for more information. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论