grzegorz-jamroz / sf-storage-api-bundle
此包最新版本(v6.1.5)没有提供许可证信息。
此包基于源文件提供Symfony Storage Api的基本功能
v6.1.5
2022-12-07 15:24 UTC
Requires
- php: >=8.1
- grzegorz-jamroz/entity-storage: ^0.0.4
- grzegorz-jamroz/sf-api-bundle: ^0.0.1
- grzegorz-jamroz/sf-api-foundation: ^6.1.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.11
- phpstan/phpstan: ^1.8
- phpunit/phpunit: ^9.5
- symfony/yaml: ^6.1
README
此包基于源文件提供Symfony Storage Api的基本功能
安装
composer require grzegorz-jamroz/sf-storage-api-bundle
基本用法
- 标记存储 - 例如
# config/services.yaml services: # ... _instanceof: # services whose classes are instances of EntityStorageInterface will be tagged automatically Ifrost\EntityStorage\Storage\EntityStorageInterface: tags: [ifrost_api.storages] # ...
- 更新项目中路由配置
# config/routes.yaml controllers: resource: ../src/Controller/ type: attribute # ... # add those lines: ifrost_storage_api_controllers: resource: ../src/Controller/ type: storage_api_attribute # ...
- 创建你的控制器
<?php declare(strict_types=1); namespace App\Controller; use App\Entity\Product; use App\Storage\ProductStorage; use Ifrost\StorageApiBundle\Attribute\StorageApi; use Ifrost\StorageApiBundle\Controller\StorageApiController; #[StorageApi(entity: Product::class, storage: ProductStorage::class, path: 'products')] class ProductController extends StorageApiController { }
- 现在你可以调试你的路由。运行命令
php bin/console debug:router
你应该得到输出
------------------- -------- -------- ------ --------------------------
Name Method Scheme Host Path
------------------- -------- -------- ------ --------------------------
_preview_error ANY ANY ANY /_error/{code}.{_format}
products_find GET ANY ANY /products
products_find_one GET ANY ANY /products/{uuid}
products_create POST ANY ANY /products
products_update PUT ANY ANY /products/{uuid}
products_modify PATCH ANY ANY /products/{uuid}
products_delete DELETE ANY ANY /products/{uuid}
------------------- -------- -------- ------ --------------------------
更多自定义用法
如果你决定要更改某些特定路由的路由配置,只需添加带有新参数的Route
属性。例如
<?php declare(strict_types=1); namespace App\Controller; use App\Entity\Product; use App\Storage\ProductStorage; use Ifrost\StorageApiBundle\Attribute\StorageApi; use Ifrost\StorageApiBundle\Controller\StorageApiController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; #[StorageApi(entity: Product::class, storage: ProductStorage::class, path: 'products')] class ProductController extends StorageApiController { #[Route('/create_products', name: 'products_create', methods: ['POST'])] public function create(): Response { return $this->getApi()->create(); } }
现在从php bin/console debug:router
的输出将是
------------------- -------- -------- ------ --------------------------
Name Method Scheme Host Path
------------------- -------- -------- ------ --------------------------
_preview_error ANY ANY ANY /_error/{code}.{_format}
products_create POST ANY ANY /create_products
products_find GET ANY ANY /products
products_find_one GET ANY ANY /products/{uuid}
products_update PUT ANY ANY /products/{uuid}
products_modify PATCH ANY ANY /products/{uuid}
products_delete DELETE ANY ANY /products/{uuid}
------------------- -------- -------- ------ --------------------------
你也可以禁用一些操作。在这种情况下,你可以使用excludedActions
元数据。
<?php declare(strict_types=1); namespace App\Controller; use App\Entity\Product; use App\Storage\ProductStorage; use Ifrost\StorageApiBundle\Attribute\StorageApi; use Ifrost\StorageApiBundle\Controller\StorageApiController; use Ifrost\ApiFoundation\Enum\Action; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; #[StorageApi( entity: Product::class, storage: ProductStorage::class, path: 'products', excludedActions: [ Action::CREATE, Action::UPDATE, Action::MODIFY, 'delete', 'not_valid_actions_will_be_omitted' ])] class ProductController extends StorageApiController { }
现在从php bin/console debug:router
的输出将是
------------------- -------- -------- ------ --------------------------
Name Method Scheme Host Path
------------------- -------- -------- ------ --------------------------
_preview_error ANY ANY ANY /_error/{code}.{_format}
products_find GET ANY ANY /products
products_find_one GET ANY ANY /products/{uuid}
------------------- -------- -------- ------ --------------------------