定制 extalion/api-platform-extensions-bundle 二次开发

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

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

extalion/api-platform-extensions-bundle

最新稳定版本:0.1.1

Composer 安装命令:

composer require extalion/api-platform-extensions-bundle

包简介

Some extensions classes/services to simplify build api

README 文档

README

Installation

Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.

Applications that use Symfony Flex

Open a command console, enter your project directory and execute:

$ composer require extalion/api-platform-extensions-bundle

Applications that don't use Symfony Flex

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

$ composer require extalion/api-platform-extensions-bundle

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles in the config/bundles.php file of your project:

// config/bundles.php

return [
    // ...
    Extalion\ApiPlatformExtensionsBundle\ApiPlatformExtensionsBundle::class => ['all' => true],
];

Usage

Custom data provider

If you want to add data provider and keep injected api-platform extensions, add custom operation to entity

<?php
// src/Entity/Book.php

use ApiPlatform\Core\Annotation\ApiResource;
use Extalion\ApiPlatformExtensionsBundle\Controller\CustomCollectionOperation;
use Extalion\ApiPlatformExtensionsBundle\Controller\CustomItemOperation;

#[ApiResource(
  collectionOperations: [
    'get_custom_collection' => [
      'method' => 'GET',
      'path' => '/custom_books',
      'controller' => CustomCollectionOperation::class,
    ],
  ],
  itemOperations: [
    'get_custom_item' => [
      'method' => 'GET',
      'path' => '/custom_books/{id}',
      'controller' => CustomItemOperation::class,
      'read' => false,
    ],
  ]
)]
class Book
{
    // ...
}

and create doctrine extension

<?php
// src/Doctrine/Extension/Operations/GetCustomBook.php

use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\ContextAwareQueryCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use App\Entity\Book;
use Doctrine\ORM\QueryBuilder;

final class GetCustomBook implements ContextAwareQueryCollectionExtensionInterface, QueryItemExtensionInterface
{
    public function applyToCollection(
        QueryBuilder $queryBuilder,
        QueryNameGeneratorInterface $queryNameGenerator,
        string $resourceClass,
        string $operationName = null,
        array $context = []
    ): void {
        if (
            $resourceClass === Book::class
            && $operationName === 'get_custom_collection'
        ) {
            $book = $queryBuilder->getRootAliases()[0];
            $queryBuilder->andWhere("{$book}.customField = :book_custom_field");
            $queryBuilder->setParameter('book_custom_field', true);
        }
    }

    public function applyToItem(
        QueryBuilder $queryBuilder,
        QueryNameGeneratorInterface $queryNameGenerator,
        string $resourceClass,
        array $identifiers,
        string $operationName = null,
        array $context = []
    ): void {
        if (
            $resourceClass === Book::class
            && $operationName === 'get_custom_item'
        ) {
            $id = $identifiers['id'];
            $book = $queryBuilder->getRootAliases()[0];
            $queryBuilder->andWhere("{$book}.id = :book_id");
            $queryBuilder->andWhere("{$book}.customField = :book_custom_field");
            $queryBuilder->setParameter('book_id', $id);
            $queryBuilder->setParameter('book_custom_field', true);
        }
    }
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-10-07