Coder Social home page Coder Social logo

spyric / laravel-fractal Goto Github PK

View Code? Open in Web Editor NEW

This project forked from spatie/laravel-fractal

0.0 2.0 0.0 167 KB

A Fractal service provider for Laravel

Home Page: https://murze.be/2015/10/a-fractal-service-provider-for-laravel/

License: MIT License

PHP 100.00%

laravel-fractal's Introduction

A Fractal service provider for Laravel

Latest Version on Packagist Software License Build Status Quality Score StyleCI Total Downloads

The package provides a nice and easy integration with Fractal for 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();

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Postcardware

You're free to use this package (it's MIT-licensed), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

The best postcards will get published on the open source page on our website.

Install

You can pull in the package via composer:

$ composer require spatie/laravel-fractal

Next up, the service provider must be registered:

// config/app.php
'providers' => [
    ...
    Spatie\Fractal\FractalServiceProvider::class,

];

If you want to make use of the facade you must install it as well:

// config/app.php
'aliases' => [
    ...
    'Fractal' => Spatie\Fractal\FractalFacade::class,
];

If you want to change the default serializer, you must publish the config file:

php artisan vendor:publish --provider="Spatie\Fractal\FractalServiceProvider"

This is the contents of the published file:

return [

    /*
    |--------------------------------------------------------------------------
    | Default Serializer
    |--------------------------------------------------------------------------
    |
    | 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' => '',

];

Usage

Refer to the documentation of spatie/fractalistic to learn all the methods this package provides.

In all code examples you may use fractal() instead of Fractal::create().

Send a response with transformed data

To return a response with json data you can to this in a Laravel app.

$books = fractal($books, new BookTransformer())->toArray();

return response()->json($books);

The respond() method on the Fractal class can make this process a bit more streamlined.

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 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',
        ]);
});

Quickly creating a transformer

You can run the make:transformer command to quickly generate a dummy transformer. By default it will be stored in the app\Transformers directory.

Upgrading

From v2 to v3

v3 was introduced to swap out the league/fractal with spatie/fractalistic. Support for Lumen was dropped. You should be able to upgrade a Laravel application from v2 to v3 without any code changes.

From v1 to v2

In most cases you can just upgrade to v2 with making none or only minor changes to your code:

  • resourceName has been renamed to withResourceName.

The main reason why v2 of this package was tagged is because v0.14 of the underlying Fractal by the League contains breaking change. If you use the League\Fractal\Serializer\JsonApiSerializer in v2 the links key will contain self, first, next and last.

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

About Spatie

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

License

The MIT License (MIT). Please see License File for more information.

laravel-fractal's People

Contributors

arubacao avatar bjrnblm avatar bretto36 avatar clarkeash avatar countless-integers avatar dbohn avatar dczajkowski avatar eblount avatar freekmurze avatar ghunti avatar hosmelq avatar maxeee09 avatar miclf avatar morloderex avatar rupertjeff avatar sebastiandedeyne avatar svenluijten avatar vmitchell85 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.