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

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

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

fd6130/fractal-bundle

最新稳定版本:4.1.1

Composer 安装命令:

composer require fd6130/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.

This is a fork version of samjarrett/FractalBundle.

Getting Started

Requirements:

  • PHP >= 7.4
  • Symfony 4, 5 and 6

Install through composer:

composer require fd6130/fractal-bundle

If you are using symfony flex, it will automatic register the bundle for you.

Usage

You can use command php bin/console make:fractal-transformer to create a transformer.

Or, just create it by your own and place it in src/Transformer.

class UserTransformer extends TransformerAbstract
{    
    public function transform(User $user): array
    {
        $data = [
            'id' => $user->id(),
            'name' => $user->name(),
        ];
        
        return $data;
    }
}

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

$response = $manager->createData($resource)->toArray();

Inject services to the transformers

You can inject services to your transformer through constructor:

class UserTransformer extends TransformerAbstract
{
    private $entityManager;
    
    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;
    }
    
    public function transform(User $user): array
    {
        $data = [
            'id' => $user->id(),
            'name' => $user->name(),
        ];

        // $this->entityManager->getRepository(...)
        
        return $data;
    }
}

统计信息

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

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 11
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-02-17