承接 samj/fractal-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

samj/fractal-bundle

最新稳定版本:4.0.0

Composer 安装命令:

composer require samj/fractal-bundle

包简介

A Symfony Bundle for Fractal by League. Implements dependency injection into your transformers

README 文档

README

This bundle provides integration of league/fractal for Symfony. In addition it allows you to use transformers as a services.

Getting Started

First of all you need to add dependency to composer.json:

composer require samj/fractal-bundle 

Then register bundle in app/AppKernel.php:

public function registerBundles() { return [ // ... new SamJ\FractalBundle\SamJFractalBundle(), ]; }

Now we can write and use fractal transformers:

Using Transformers as Services

There are several cases when you need to pass some dependencies into transformer. The common one is role/scope based results in transformers. For example you need to show email field only for administrators or owner of user profile:

class UserTransformer extends TransformerAbstract { private $authorizationCheker; public function __construct(AuthorizationChecker $authorizationCheker) { $this->authorizationCheker = $authorizationCheker; } public function transform(User $user) { $data = [ 'id' => $user->id(), 'name' => $user->name(), ]; if ($this->authorizationChecker->isGranted(UserVoter::SEE_EMAIL, $user)) { $data['email'] = $user->email(); } return $data; } }

Then you could just register this class as service, and pass service ID as transformer. This bundle then will try to get it from container:

$resource = new Collection($users, 'app.transformer.user');

This works in includes as well:

public function includeFriends(User $user) { return $this->collection($user->friends(), 'app.transformer.user'); }

You could see example of how to use transformers in sample application which is used in test suites.

统计信息

  • 总下载量: 314.96k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 29
  • 点击次数: 1
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 28
  • Watchers: 2
  • Forks: 10
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04