laravel5-fractal, 一种简单的Laravel 5分形服务提供者和变压器

分享于 

5分钟阅读

GitHub

  繁體 雙語
A simple fractal service provider and transformer generator for laravel 5
  • 源代码名称:laravel5-fractal
  • 源代码网址:http://www.github.com/Cyvelnet/laravel5-fractal
  • laravel5-fractal源代码文档
  • laravel5-fractal源代码下载
  • Git URL:
    git://www.github.com/Cyvelnet/laravel5-fractal.git
    Git Clone代码到本地:
    git clone http://www.github.com/Cyvelnet/laravel5-fractal
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/Cyvelnet/laravel5-fractal
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    

    StyleCIBuild StatusTotal DownloadsLatest Stable VersionLatest Unstable VersionLicense

    laravel5-fractal

    一种 Laravel 5和流明的简单分形服务提供者和变压器

    安装

    Laravel

    使用以下命令要求使用 Composer 软件包:

    composer require cyvelnet/laravel5-fractal

    更新 Composer 后,将ServiceProvider添加到配置/app。php中的提供程序的array 中

    CyvelnetLaravel5FractalLaravel5FractalServiceProvider::class,

    和 register Facade,并可以选择向 aliases array 添加新行:

    '分形'=> CyvelnetLaravel5FractalFacadesFractal::class

    /bootstrap/app.php 中的register 服务提供程序

    $app->register(CyvelnetLaravel5FractalLaravel5FractalServiceProvider::class);

    取消注释行

    $app->withFacades();

    最后是 register的Facade

    class_alias(CyvelnetLaravel5FractalFacadesFractal::class, 'Fractal');

    配置

    你还可以发布配置文件以更改实现以适合你。

    php artisan vendor:publish --provider="CyvelnetLaravel5FractalLaravel5FractalServiceProvider"
    自动子资源注入。

    默认情况下禁用自动插入/嵌入子资源,以启用这里功能,编辑 config/fractal.php 并设置

    autoload => true

    命令

    如果你的api是有帮助的,那么你就可以在你的转换中自动添加你的模型属性了。

    //generate a empty transformer
     php artisan make:transformer UserTransformer
    //generate a modeled transformer
     php artisan make:transformer UserTransformer -m User

    用法

    Fractal::item() ;

    转换单个记录

    $user=User::find(1);Fractal::item($user, newUserTransformer());

    Fractal::collection() ;

    转换记录集合

    $users=User::where('activated', true)->get();// $resourceKey is optional for most serializer, but recommended to set for JsonApiSerializer$resourceKey='user';Fractal::collection($users, newUserTransformer(), $resourceKey);

    Fractal::includes( )

    插入子资源

    Fractal::includes('orders') // where 'orders' is defined in your transformer class's $availableIncludes array

    Fractal::excludes( )

    删除子资源

    Fractal::excludes('orders')

    Fractal::setSerializer( )

    更改变压器序列化程序

    Fractal::setSerializer(AcmeMySerializer); // where MySerializer is a class extends LeagueFractalSerializerSerializerAbstract 

    Fractal::fieldsets( )

    添加稀疏字段集

    Fractal::fieldsets(['orders'=>'item,qty,total,date_order'])

    Fractal::addMeta( )

    向 root 添加额外的元数据

    // specify with single meta dataFractal::addMeta($key='metaKey', $data='metaData')// add an array of meta dataFractal::addMeta(['key1'=>'data1','key2'=>'data2' ])

    特性

    https://github.com/Cyvelnet/laravel5-fractal/wiki/Transformable-Trait

    https://github.com/Cyvelnet/laravel5-fractal/wiki/Custom-TransformerableAbstract-class


    相关文章