Download the PHP package requestum/symfony-api-edition without Composer
On this page you can find all versions of the php package requestum/symfony-api-edition. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download requestum/symfony-api-edition
More information about requestum/symfony-api-edition
Files in requestum/symfony-api-edition
Package symfony-api-edition
Short Description Symfony edition for api development
License MIT
Informations about the package symfony-api-edition
Docs
/api/doc
Authentication
Api uses OAuth2 authentication. To authenticate your request add "Authorization" header with value "Bearer access_token"
Fixtures
User 1\ username: [email protected]\ password: 123
User 2\ username: [email protected]\ password: 123
Access Tokens: Access_Token_For_Artur, Access_Token_For_Kirill
Basic concepts
This bundle provides ready to use action classes for standard REST API operations such as create, update, delete, list and transit. It uses class per action approach, so each action type has it's own class. Also this bundle utilizes concept of having many services (i.e. class instances) per one action class.
Imagine we have 3 endpoints that provide a list of resources. ` Each of these endpoints has same business logic and can be covered by the same code. We have to query database, apply some filters, pagination and sorting. Then we have to prepare result data, serialize it and send back to the client. The only difference here is that we have different resource entity repository for each endpoint. Also these almost same endpoints can have different values for some parameters such as available filters, default page size, etc. So the idea is to have unified parametrized list action class, and instantiate it three times with different arguments. For more flexibility in parametrization we use Symfony OptionResolver component. So each action class has a set of options to configure concrete endpoint.
Under the hood
This bundle consist of next components:
- Resource metadata factory
- Serializer extension with next features
- expand of entity relations on demand
- access control per field during serialization
- Event listeners that handle frequent use cases in api development
- json decoder that populates HttpFoundation request object's request parameter bag with passed data
- exception listener that formats errors
- Error factory
- Base class for voters that vote on concrete resource entity
- Action classes
Action classes
- List
- Fetch
- Create
- Update
-
Delete
Create operation
List action
Get list of items. \ Object type: collection \ HTTP method: GET
Configuration
1 Add service. Example:
Where: \
arguments: ...
- required arguments to the Requestum\ApiBundle\Action\ListAction
class constructor \
- MyProject\MyBundle\Entity\Сountry
- entity class (required) \
['setOptions', ...]
- array of options
2 Add service to routing. Example:
Available Options
Option | Type | Default value | Description |
---|---|---|---|
default_per_page | integer | 20 | Results per page (Pagination) |
pagerfanta_fetch_join_collection | boolean | false | Whether the query joins a collection join collection (Pagination) |
pagerfanta_use_output_walkers | boolean | null | Whether the query joins a collection join collection (Pagination) |
serialization_groups | array | ['default'] | One can serialize properties that belong to chosen groups only |
serialization_check_access | boolean | true | Check user access during serialization |
filters | array | [] | Filtering results (More information) |
preset_filters | array | [] | Preset filters and values. String value can be used as alias for the current authorized user. |
Filters
Query filter\ Available text search in some fields (). Supports wildcards (, , ) \ To add fields you need to edit the method in the entity repository. \ Add a filter using option. \ Example:
Sample query with filter:
You can specify particular fields you want to search in (from list you passed to SearchHandler).
Sorting \ One may add the property name and sort order to the request (pattern: 'field|order') to sort. Example:
Filter by properties \ Such filtering by entity is available:
- exact matching (Example: );
- using comparison operators (`` etc.) and , ,
(Example: )
To change the filtering logic by association entities or existing filters, one may to make changes to the method in the entity repository. Example:
Custom filter \ To create custom filters one need: \ 1 Add new Handler. Example:
2 Add handler to item repository. Example:
3 Add custom filter to service. Example:
Additional functionality
Pagination
Pagerfanta is used for pagination and works with DoctrineORM query objects only. \ ApiBundle pagination configured with default options and (This setting can be changed in options). \ One use pagination add and to the request.\ Example:
Count only
To get the count of query results only one may add to the request attributes. Add to routing configuration as an example:
Expand
One can use the related entity references instead of full value in the response (can be expanded on demand) by adding annotation to entity property, for example:
Add to the request for expand reference. For multiple references expansion according fields should be separated be commas(NB: no spaces needs here!). One use the point for expand the field in associated entity. \ Example:
Request example
Response example
Fetch action
Get single item by identifier. \ Object type: item \ HTTP method: GET
Configuration
1 Add service. Example:
Where: \
arguments: ...
- required arguments to the Requestum\ApiBundle\Action\FetchAction
class constructor \
- MyProject\MyBundle\Entity\Сountry
- entity class (required) \
['setOptions', ...]
- array of options
2 Add service to routing. Example:
Available Options
Option | Type | Default value | Description |
---|---|---|---|
serialization_groups | array | ['default'] | One can serialize properties that belong to chosen groups only |
serialization_check_access | boolean | true | Check user access during serialization |
fetch_field | string/array | 'id' | Possibility to use one (string) or more (array) property of entity as an unique identifier |
access_attribute | string | 'fetch' | Access attribute for check user permissions (More information) |
Access attribute
Symfony Voters are used for check the user's access permissions. AccessDecisionManager
will receive value of access_attribute
as $attribute
and entity as subject. \
Bundle provides the base class AbstractEntityVoter
, which checks the user in the session depending on the received parameter $userRequired
(optional, true
by default).
It easy to use with the following settings for access_decision_manager
:
Also the bundle has a OwnerVoter
class that working with [update, delete] attributes.
It uses the Symfony PropertyAccess Component for check the current user's relationship (is the owner)
to the subject entity. The relationships checked by $propertyPath
which is passed to the constructor for OwnerVoter
class. \
One can create custom voters based on the AbstractEntityVoter
class. Example:
1 Add new voter:
2 Add new voter to services:
Where: \
arguments: ...
- arguments to the custom voter class constructor \
[fetch, create, update, delete]
- array of access attributes (required) \
YourBundle\Entity\Country
- entity class (required) \
true
- required user flag (optional, true
by default)
3 Add 'access_attribute'
to service config for set attributes to check user permissions (as needed). \
'access_attribute' : 'fetch'
by default.
Additional functionality
Expand
One can use the related entity references instead of full value in the response. See Expand in ListAction
Request example
Response example
Abstract Form Action Class
This is an abstract class that is the parent for the Update Actions. Can be used to inherit and to create another custom actions.
Available Options
Option | Type | Description | Default Values |
---|---|---|---|
http_method | string | HTTP method | POST |
success_status_code | integer | Status that is returned after execution | 200 |
return_entity | boolean | Result entity in response | true |
form_options | array | options that will be used in building form | [] |
before_save_events | array | Before submit events (events that throws before the flush) | [] |
after_save_events | array | After submit events (events that throws after the flush) | [] |
Create Action
Action to create a new object. This is a subclass that inherits from AbstractFormAction class.
There are two required parameters: Entity class and FormType Class. Example:
Available Options
Option | Type | Description | Default Values |
---|---|---|---|
http_method | string | HTTP method | POST |
success_status_code | integer | Status that is returned after execution | 201 |
return_entity | boolean | Result entity in response | true |
form_options | array | options that will be used in building form | [] |
before_save_events | array | Before submit events (events that throws before the flush) | [] |
after_save_events | array | After submit events (events that throws after the flush) | [] |
access_attribute | string | Access Attribute | create |
Event listeners
By default Create and Update actions throws such events: 'action.before_save'
, 'action.after_save'
. You can dispatch this events, or throw another events using such options as: before_save_events
and after_save_events
.
You can create listeners that will respond to event occuring before and after submit the request.
You need to configure it in services.yml
file:
Then you need to specify this listeners in create action configuration:
Update Action
Action to update an existing object. This is a subclass that inherits from AbstractFormAction class.
There are two required parameters: Entity class and FormType Class. Example:
Available Options
Option | Type | Description | Default Values |
---|---|---|---|
http_method | string | HTTP method | PATCH |
success_status_code | integer | Status that is returned after execution | 200 |
return_entity | boolean | Result entity in response | true |
form_options | array | options that will be used in building form | [] |
before_save_events | array | Before submit events (events that throws before the flush) | [] |
after_save_events | array | After submit events (events that throws after the flush) | [] |
access_attribute | string | Access Attribute | update |
Update action has the same available features and options as a create action. (see "Create Action")
Delete Action
Action to delete an existing object
There is one required parameter: Entity class. Example:
Available Options
Option | Type | Description | Default Values |
---|---|---|---|
fetch_field | string | The field that is the entity identifier (id by default) | id |
before_delete_events | array | Before delete events | [] |
access_attribute | string | Access Attribute | delete |
Event listeners
By default Delete action throws a such event: 'action.before_delete'
. You can dispatch this event, or throw another events using such an option: before_delete_events
.
You can create listeners that will respond to event occuring before delete the entity.
You need to configure it in services.yml
file:
Then you need to specify this listeners in delete action configuration:
All versions of symfony-api-edition with dependencies
doctrine/doctrine-bundle Version ^1.6
doctrine/doctrine-fixtures-bundle Version ^2.4
doctrine/orm Version ^2.5
friendsofsymfony/oauth-server-bundle Version ^1.5
guzzlehttp/guzzle Version ^6.0
incenteev/composer-parameter-handler Version ^2.0
jms/serializer Version ^1.13
league/oauth2-server Version ^7.2
nelmio/cors-bundle Version ^1.4
oneup/uploader-bundle Version ^1.8
phpdocumentor/reflection-docblock Version ^3.0
requestum/api-bundle Version dev-master@dev
requestum/email-sender-bundle Version dev-master@dev
sensio/distribution-bundle Version ^5.0.19
sensio/framework-extra-bundle Version ^3.0.2
stof/doctrine-extensions-bundle Version ^1.2
symfony/monolog-bundle Version ^3.1.0
symfony/swiftmailer-bundle Version ^2.3.10
symfony/symfony Version 3.3.*
twig/twig Version ^1.0 || ^2.0
white-october/pagerfanta-bundle Version ^1.0