paysera / lib-rest-bundle
Helps easily convert your regular controller to RESTful controller
Installs: 25 805
Dependents: 2
Suggesters: 0
Security: 0
Stars: 3
Watchers: 10
Forks: 11
Open Issues: 0
Requires
- php: >=7.4
- doctrine/orm: ^2.0
- doctrine/persistence: ^3.2
- paysera/lib-serializer: ^3.0
- symfony/config: ^4.0 || ^5.0
- symfony/dependency-injection: ^4.0 || ^5.0
- symfony/http-foundation: ^4.0 || ^5.0
- symfony/http-kernel: ^4.0 || ^5.0
- symfony/routing: ^4.0 || ^5.0
- symfony/security-core: ^4.0 || ^5.0
- symfony/validator: ^4.0 || ^5.0
Requires (Dev)
- mockery/mockery: ^1.2
- phpunit/phpunit: ^8.0
README
Deprecated, use https://github.com/paysera/lib-api-bundle instead.
This bundle provides means for rapid API development.
Installation
- Download bundle:
composer require paysera/lib-rest-bundle
- Enable bundle:
class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new Paysera\Bundle\RestBundle\PayseraRestBundle(), ); // ... } }
Configuration
- Example of the full bundle configuration
paysera_rest: property_path_converter: 'my_custom_path_converter' # Overrides the default path converter locales: ['en', 'ru', 'lt'] # Optional list of accepted locales
Basic Usage
- Create and configure your controller:
class ApiController { public function saveData(Data $data) { ... return new CustomResponseEntity(); } }
<service id="app_bundle.controller.api_controller" class="AppBundle\Controller\ApiController" public="true"> ... </service>
- Create API service:
<service id="app_bundle.service.api_service" class="Paysera\Bundle\RestBundle\RestApi"> <tag name="paysera_rest.api" api_key="my_custom_api_key"/> <argument type="service" id="service_container"/> <argument type="service" id="logger"/> </service>
- Configure your routing and add
api_key
:
<route id="my_api_route.post_data" path="/resource" methods="POST"> <default key="_controller">app_bundle.controller.api_controller:saveData</default> <default key="api_key">my_custom_api_key</default> </route>
- Optionally, add request and response mappers to your API service:
<service id="app_bundle.service.api_service" class="Paysera\Bundle\RestBundle\RestApi"> <tag name="paysera_rest.api" api_key="my_custom_api_key"/> <argument type="service" id="service_container"/> <argument type="service" id="logger"/> <call method="addRequestMapper"> <argument>app_bundle.normalizer.data</argument> <argument>app_bundle.controller.api_controller:saveData</argument> <argument>data</argument> </call> <call method="addResponseMapper"> <argument>app_bundle.normalizer.custom_response</argument> <argument>app_bundle.controller.api_controller:saveData</argument> </call> </service>
Here app_bundle.normalizer.data
is a service implementing \Paysera\Component\Serializer\Normalizer\DenormalizerInterface
, app_bundle.normalizer.custom_response
is a service implementing \Paysera\Component\Serializer\Normalizer\NormalizerInterface
. Both of these services must be configured as public in their service definitions.
Testing
php7.4
docker build -t lib-rest7.4 -f docker/Dockerfile_PHP7.4 .
docker run -it -u $UID -v $PWD:/app -w /app lib-rest7.4 composer i
docker run -it -u $UID -v $PWD:/app -w /app lib-rest7.4 bin/phpunit
php8.0
docker build -t lib-rest8.0 -f docker/Dockerfile_PHP8.0 .
docker run -it -u $UID -v $PWD:/app -w /app lib-rest8.0 composer i
docker run -it -u $UID -v $PWD:/app -w /app lib-rest8.0 bin/phpunit
debugging
php7.4
docker run --add-host=host.docker.internal:host-gateway -e XDEBUG_MODE=debug -it -u $UID -v $PWD:/app -w /app lib-rest7.4 ...
php8.0
docker run --add-host=host.docker.internal:host-gateway -e XDEBUG_MODE=debug -it -u $UID -v $PWD:/app -w /app lib-rest8.0 ...