Slide 1

Slide 1 text

API Platform API Driven Development - The easy way

Slide 2

Slide 2 text

Frank Jogeleit Software developer at move:elevator

Slide 3

Slide 3 text

Agenda • Introduction • Why I am talking about APIs • What am I talking about? • Example Application • Basic Features • Swagger UI • CRUD Operations • Filters • Validation First Part

Slide 4

Slide 4 text

Agenda • Advanced Features • Extensions • Serialization Context • Custom Operations • Access Control • Authentication with JsonWebToken Second Part

Slide 5

Slide 5 text

Introduction Why I am talking about APIs

Slide 6

Slide 6 text

Why I am talking about APIs The Web is growing and changing every day • More and more web-based applications are 
 single page applications • Big applications are splitting into micro services • Applications have to communicate with other external applications The need of API driven (PHP) Projects is
 growing every day

Slide 7

Slide 7 text

Introduction What am I talking about?

Slide 8

Slide 8 text

„API Platform is a next- generation web framework designed to easily create API-first projects without compromising extensibility and flexibility.“

Slide 9

Slide 9 text

API Platform • API driven Project on top of Symfony • Requires Doctrine ORM for database interactions • Define your API endpoints with minimal configuration • Supports different API Formats like 
 GraphQL, JSON-LD, JSON, XML and many more • Auto generated API documentation with Swagger UI • Supports testing with an API testing tool on top of Behat • Detailed documentation: https://api-platform.com Description

Slide 10

Slide 10 text

API Platform The default way of creating a new API Platform using Composer: • You get an preconfigured Symfony installation with the API Platform dependencies and configurations to get started The alternative way is using the symfony-flex recipe in a new or existing Symfony Flex project (more about symfony flex in the upcoming PHPDD UG talk) • You get all needed dependencies and configurations to get started Installation

Slide 11

Slide 11 text

API Platform • API Platform models have to be configured as a resource • API Platform supports YAML-, XML- and Annotation based configuration • The minimal configuration is:
 
 
 • it creates the basic CRUD Operations • adds the model in the Swagger UI Basic Configuration

Slide 12

Slide 12 text

Example Application Learning by doing

Slide 13

Slide 13 text

Learning by Doing • YAML based configuration • Configuration examples of: API Platform, Validation, Serialization, Doctrine Entities • Includes Authorization, Custom Operations, Extensions, Relation-Handling and subresources https://github.com/fjogeleit/api-platform-example

Slide 14

Slide 14 text

Basic Features Swagger UI, CRUD Operations, Filters, Validation

Slide 15

Slide 15 text

Basic Features • Auto generated API documentation including operation description, expected request (structure), configured response at success or error • Sandbox functionality to execute and test your API endpoints • Overview of defined models and serialization groups Swagger UI

Slide 16

Slide 16 text

Basic Features • Default Endpoints per Model • GET: [/models] paginated list, 30 items per page • POST: [/models] create new model • return the new entry • GET: [/models/{id}] single model representation • PUT: [/models/{id}] replace/update model • return the updated model • DELETE: [/models/{id}] delete the model CRUD Operations

Slide 17

Slide 17 text

Basic Features • If you explicitly configure the operations of your model, all operations you don’t configure are disabled • If your model has relations, you can configure them as sub-resources to generate endpoints for these relations CRUD Operations

Slide 18

Slide 18 text

Basic Features • Filters for collection operations could be declared as Symfony services and have to be configured in the resource configuration Filter Configuration

Slide 19

Slide 19 text

Basic Features • Different types of filters: • SearchFilter - Searching for exact or partial accordance (conforms with MySQL LIKE Operation) • DateFilter - Searching values in Date(Time) Intervals • BooleanFilter - Searching by bool values • NumericFilter - Searching by numeric values • RangeFilter - Search Numeric values in Numeric ranges like greater than, lower than or between • OrderFilter - Allow Sorting Available Filter

Slide 20

Slide 20 text

Basic Features • API Validation uses the Symfony Validation Component • configuration same as default Symfony projects • available Formats are YAML, XML, Annotations • Validation has to be activated 
 in the framework configuration • Required fields are marked in the Swagger UI Validation

Slide 21

Slide 21 text

Basic Features • Set name and description as required for categories Validation Example

Slide 22

Slide 22 text

Advanced Features Extensions, Serialization Context, 
 Custom Operations, Access Control

Slide 23

Slide 23 text

Advanced Features • Extensions are simple PHP classes using different interfaces for extending collection or item based operations. Extensions are configured as Symfony services • It allows you to add global filters or dynamic filters depending on user roles or something like that • A single extension is globally defined, 
 for every model. The query context is
 part of the executed method arguments Extensions

Slide 24

Slide 24 text

Advanced Features • Filter by all soft deleted products • Filter in collection- and item operations Extension Example

Slide 25

Slide 25 text

Advanced Features • By default all fields of your model are serialized and in the response of your GET requests • By default it is also possible to set or overwrite every field of model in POST or PUT requests • With serialization groups you can change this. • You can create and configure serializer groups for normalization (GET requests) and denormalization (PUT-, POST-, PATCH requests) • The configuration can be set per model or restricted for a single operation Serializer Context

Slide 26

Slide 26 text

Advanced Features • serialization groups allow you to create relations in the same post request of your main model, it also allows API Platform to return embedded relation fields in GET requests • To create serialization groups, use the 
 Symfony Serializer Component • You have to enable this feature and can use YAML, XML or Annotations for configuration Serializer Context

Slide 27

Slide 27 text

Advanced Features • Return different fields in collection and item GET request. • remove unused fields in both operations • remove unused fields from the list operation Serializer Context Example

Slide 28

Slide 28 text

Advanced Features • You can create your own operations (actions) and add them to your API model • You can create custom collection operations. These operations have no single model (pre selected by ID) as context. The path of this operation has no ID parameter.
 (Like POST: /categories) • You can also create custom item operations. These operations have a single model as argument, selected by the required ID parameter. (LIKE PUT /categories/{id}) • The Response of these operations can be an array, a single model, a collection of models or a Response object Custom Operations

Slide 29

Slide 29 text

Advanced Features • Custom operation to get informations about the authenticated user Custom Operation Example

Slide 30

Slide 30 text

Advanced Features • Restrict the access of routes or route patterns with the Symfony Security Component and the access control list • Alternatively you can restrict the access of single operations by using the access_control configuration from API Platform • It allows restriction by user roles with is_granted(ROLE) • Dynamic string based expressions using the Symfony ExpressionLanguage Component Access Control

Slide 31

Slide 31 text

Advanced Features • Restrict the Warehouse API to Admins
 
 
 • Restrict the Current User API for authenticated requests Access Control Example

Slide 32

Slide 32 text

Authentication with JsonWebToken

Slide 33

Slide 33 text

JsonWebToken • JsonWebToken (JWT) is an API based authentication like OAuth • Your API provides a login endpoint to generate your token • This endpoint requires valid user credentials (username and password) • If the login is successful you get a valid json response with your auth-token • To send authenticated requests send this token with the Bearer Prefix as Authorization header in your request • Finish How it works

Slide 34

Slide 34 text

Symfony Implementation • Symfony Implementation for JWT • Well supported by API Platform • Provide a Community Recipe for Symfony Flex • Easy to configure and use • Requires a configured User Provider • self configured with the EntityProvider (recommended) • using Bundles like the FOSUserBundle LexikJWTAuthenticationBundle

Slide 35

Slide 35 text

Symfony Implementation • Install via Composer
 
 • Create certificates
 
 
 • Configure routing LexikJWTAuthenticationBundle

Slide 36

Slide 36 text

Symfony Implementation • Configure the Firewall
 
 
 
 
 
 
 
 
 
 
 That’s it. The full example, including User Entity and Provider can be found in the example application LexikJWTAuthenticationBundle

Slide 37

Slide 37 text

Questions?

Slide 38

Slide 38 text

Resources • API Platform Documentation: 
 https://api-platform.com/ • Github: LexikJWTAuthenticationBundle:
 https://github.com/lexik/LexikJWTAuthenticationBundle • Symfony Entity Provider:
 https://symfony.com/doc/current/security/entity_provider.html • API Platform Example:
 https://github.com/fjogeleit/api-platform-example Content Resources