定制 somnambulist/fractal-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

somnambulist/fractal-bundle

最新稳定版本:2.1.2

Composer 安装命令:

composer require somnambulist/fractal-bundle

包简介

A port of samj/fractal-bundle for integrating TheLeague Fractal into Symfony

README 文档

README

GitHub Actions Build Status Issues License PHP Version Current Version

A fork and re-write of samj/fractal-bundle to provide Fractal integration with the Symfony Framework.

Requirements

  • PHP 8.2+
  • symfony/framework-bundle 6.4+

Installation

Install using composer, or checkout / pull the files from github.com.

  • composer require somnambulist/fractal-bundle

Usage

Add the SomnambulistFractalBundle to your bundles.php list if not registered by Symfony Flex.

Using Transformers as Services

This bundle allows auto-wiring / auto-configuring transformers as services. This allows you to take advantage of Symfonys container to resolve dependencies and reference transformers by class name (or service alias).

So long as the transformer extends from League\Fractal\TransformerAbstract or is tagged with somnambulist.fractal_bundle.transformer, it will be available to the Fractal Manager instance.

services: App\Http\Api\Transformers\: resource: '%kernel.project_dir%/Http/Api/Transformers/'

If transformers don't extend the TransformerAbstract be sure to tag them:

services: App\Http\Api\Transformers\: resource: '%kernel.project_dir%/Http/Api/Transformers/' tags: ['somnambulist.fractal_bundle.transformer']

Note: if your transformer is not registered as a service or passed as a valid callable or instance of TransformerAbstract, this library will raise an exception.

For example: to add an auth check to a UserTransformer (example from samj readme):

use League\Fractal\TransformerAbstract; class UserTransformer extends TransformerAbstract { public function __construct(private AuthorizationChecker $authorizationChecker) { } 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; } }

Reference the transformer by either a service alias name, or the class name:

$resource = new Collection($users, UserTransformer::class);

This works in includes as well:

public function includeFriends(User $user) { return $this->collection($user->friends(), UserTransformer::class); }

Look in the sample application for some further examples.

Tests

PHPUnit 9+ is used for testing. Run tests via vendor/bin/phpunit.

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

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