gathercontent / laravel-fractal
Laravel Service Provider for Fractal
1.0.1
2016-11-29 14:39 UTC
Requires
- php: >=5.3.0
- league/fractal: ~0.8
Requires (Dev)
- illuminate/support: 4.1.*
This package is not auto-updated.
Last update: 2024-09-11 12:19:29 UTC
README
Laravel Service Provider for League/Fractal.
安装
将 laravel-fractal 添加到您的 composer.json 文件中
"require": { "gathercontent/laravel-fractal": "~1.0" }
让 composer 安装包
$ composer require gathercontent/laravel-fractal
注册包
在 app/config/app.php
文件中的 providers
数组中注册服务提供者
'providers' => array( // ... GatherContent\LaravelFractal\LaravelFractalServiceProvider::class )
在 app/config/app.php
文件中的 aliases
数组中添加别名
'aliases' => array( // ... 'Fractal' => GatherContent\LaravelFractal\LaravelFractalFacade::class, )
配置
要覆盖默认配置,可以将配置文件发布到您的应用程序中。Artisan 可以通过命令行自动执行此操作
$ php artisan vendor:publish
用法
基本示例
格式化单个项目
// routes.php Route::get('/me', array('before' => 'auth', function () { return Fractal::item(Auth::user(), new UserTransformer); }));
格式化集合
// routes.php Route::get('/comments', function () { return Fractal::collection(Comment::all(), new CommentTransformer); });
添加元数据
// routes.php Route::get('/comments', function () { return Fractal::collection(Comment::all(), new CommentTransformer, function ($resources) { $resources->setMetaValue('foo', 'bar'); }); });
返回分页集合
// routes.php Route::get('/comments', function () { return Fractal::collection(Comment::paginate(), new CommentTransformer); });
使用自定义分页适配器
// routes.php Route::get('/comments', function () { $comments = Comment::paginate(); $adapter = new MyIlluminatePaginationAdapter($comments); return Fractal::collection($comments, new CommentTransformer, null, $adapter); });