grzegorz-jamroz / sf-storage-api-bundle
There is no license information available for the latest version (v6.1.5) of this package.
Bundle provides basic features for Symfony Storage Api base on source files
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
Bundle provides basic features for Symfony Storage Api base on source files
Installation
composer require grzegorz-jamroz/sf-storage-api-bundle
Base Usage
- Tag storages - for example:
# config/services.yaml services: # ... _instanceof: # services whose classes are instances of EntityStorageInterface will be tagged automatically Ifrost\EntityStorage\Storage\EntityStorageInterface: tags: [ifrost_api.storages] # ...
- Update routing configuration in your project:
# config/routes.yaml controllers: resource: ../src/Controller/ type: attribute # ... # add those lines: ifrost_storage_api_controllers: resource: ../src/Controller/ type: storage_api_attribute # ...
- Create your controller:
<?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 { }
- Now you can debug your routes. Run command:
php bin/console debug:router
you should get output:
------------------- -------- -------- ------ --------------------------
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}
------------------- -------- -------- ------ --------------------------
More custom usage
If you decided that you want to change routing configuration for some specific route just add Route
attribute with new parameters. For example:
<?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(); } }
now output from php bin/console debug:router
will be:
------------------- -------- -------- ------ --------------------------
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}
------------------- -------- -------- ------ --------------------------
It is possible do disable some actions at all. In this case you can use excludedActions
metadata.
<?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 { }
now output from php bin/console debug:router
will be:
------------------- -------- -------- ------ --------------------------
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}
------------------- -------- -------- ------ --------------------------