SlideShare a Scribd company logo
Creating a modern web
application using
Symfony API Platform
by Jesus Manuel Olivas / weKnow
@jmolivas
● $ whoami
Jesus Manuel Olivas
jmolivas@weknowinc.com
jmolivas
jmolivas
drupal.org/u/jmolivas
jmolivas.weknowinc.com
Mexicali

Mexico + California
Calexico

California + Mexico
$ ifconfig
WeAre
WeKnow
WeGive
2,572,697
Drupal … Drupal … Drupal
When all you have is a
hammer, everything looks
like a nail.
Creating a modern web application using  Symfony API Platform Atlanta
Traditional Monolithic CMS
Drupal Headless API + Front-end library
● Fetch
What if … you do not need
> Complex content model (paragraphs)
> Editorial workflow
> Revisions
> GUI to manage Queries (views)
> GUI to manage content model
> Widgets and formatters
Symfony API + Front-end library/framework
● API / GraphQL
Symfony
API Platform
GraphQL
 ReactJS
Symfony Flex
Symfony Flex … a Composer plugin for Symfony
> Symfony Flex is a Composer plugin that modifies the behavior of
the require, update, and remove composer commands.
> Symfony Flex automates the most common tasks of Symfony
applications, like installing and removing bundles and other
dependencies using recipes defined in a manifest.json file.
Directory structure
API Platform 
Framework
The API Platform Framework
REST and GraphQL
framework to build
modern API-driven
projects
https://api-platform.com/
Built on the Shoulders of Giants
> Extend the framework with thousands of existing Symfony
bundles and React components.
> The API component includes the Symfony 4 flex, the Doctrine ORM.
Client-side components and Admin based on React and a Docker
configuration ready to startup your project using one single command.
> Reuse all your Symfony, React and Docker skills and benefit of their high
quality docs; you are in known territory.
The API Platform Components
API Schema Admin CRUD
Try API-Platform
# Clone code repository

git clone https://github.com/api-platform/api-platform.git
Recommendations and adjustments
> Update route prefix at api/config/routes/api_platform.yaml
file.
api_platform:

…

prefix: /api
> Update admin/.env and client/.env files (change protocol and port).
REACT_APP_API_ENTRYPOINT=http://localhost:8080/api
Start containers … and grab water, coffee, or a beer.
# Start containers
docker-compose up
# Open browser
open http://localhost/
Add more formats
Update api/config/packages/api_platform.yaml adding:
formats:
jsonld: [‘application/ld+json'] # first one is the default format
json: ['application/json']
jsonhal: ['application/hal+json']
xml: ['application/xml', 'text/xml']
yaml: ['application/x-yaml']
csv: ['text/csv']
html: ['text/html']
Remove default & add new entities
> Remove default entity

api/src/Entity/Greeting.php
> Add new entities to api/src/Entity/ directory:

console make:entity
Creating a modern web application using  Symfony API Platform Atlanta
Creating a modern web application using  Symfony API Platform Atlanta
Creating a modern web application using  Symfony API Platform Atlanta
Creating a modern web application using  Symfony API Platform Atlanta
api/src/Entity/Post.php 1/3
<?php

namespace AppEntity;



use ApiPlatformCoreAnnotationApiResource;

use DoctrineORMMapping as ORM;

use SymfonyComponentValidatorConstraints as Assert;
/**

* @ApiResource

* @ORMTable(name="post")

* @ORMEntity

*/
class Post
{
…
}
api/src/Entity/Post.php 2/3
/**

* @ORMId

* @ORMGeneratedValue(strategy="AUTO")

* @ORMColumn(type="integer")

*/

private $id;
/**

* @ORMColumn

* @AssertNotBlank

*/

public $title = '';

/**

* @ORMColumn
* @AssertNotBlank
*/
public $body = '';
api/src/Entity/Post.php 3/3
/**
* @ORMManyToOne(targetEntity="PostType")
* @ORMJoinColumn(name="post_type_id", referencedColumnName="id", nullable=false)
*/
public $type;
public function getId(): int
{
return $this->id;
}
Tracking Database changes
# Add dependency
composer require migrations
# Execute command(s)
console make:migration
console doctrine:migrations:migrate
Add FOSUserBundle
# Add dependency
composer require friendsofsymfony/user-bundle
composer require symfony/swiftmailer-bundle
https://symfony.com/doc/current/bundles/FOSUserBundle/index.html
https://jolicode.com/blog/do-not-use-fosuserbundle
Loading Posts using the Browser
http://localhost:8080/api/posts
http://localhost:8080/api/posts.json
http://localhost:8080/api/posts.jsonld
http://localhost:8080/api/posts/1
http://localhost:8080/api/posts/1.json
Loading Posts using the CLI
curl -X GET "http://localhost:8080/api/posts" 
-H "accept: application/json"
curl -X GET "http://localhost:8080/api/posts/1" 
-H "accept: application/ld+json"
ADD Posts from CLI
curl -X POST "http://localhost:8080/api/posts" 
-H "accept: application/ld+json" 
-H "Content-Type: application/ld+json" 
-d '{ "title": "Post create from CLI", "body": "body-
less", "type": "/api/post_types/1"}'
UPDATE and REMOVE Posts from CLI
curl -X PUT "http://localhost:8080/api/posts/9" 
-H "accept: application/ld+json" 
-H "Content-Type: application/ld+json" 
-d '{ "title": "Updated from CLI"}'
curl -X DELETE "http://localhost:8080/api/posts/10" 
-H "accept: application/json"
Serialization
> API Platform allows to specify the which attributes of the resource are
exposed during the normalization (read) and denormalization (write)
process. It relies on the serialization (and deserialization) groups feature of
the Symfony Serializer component.
> In addition to groups, you can use any option supported by the Symfony
Serializer such as enable_max_depth to limit the serialization depth.
Serialization Relations (Post => PostType) 1/2
# api/src/Entity/Post.php & PostType.php
* @ApiResource(attributes={
* "normalization_context"={"groups"={"read"}},
* "denormalization_context"={"groups"={"write"}}
* })
Serialization Relations (Post => PostType) 2/2
# Add use keyword to class
use SymfonyComponentSerializerAnnotationGroups;
# Add use keyword to properties
* @Groups({"read"})
* @Groups({"read", "write"})
GraphQL
A query language for your API
GraphQL
GraphQL offers significantly more flexibility for integrators.
Allows you to define in detail the only the data you want.
GraphQL lets you replace multiple REST requests with a single
call to fetch the data you specify.
Add GraphQL
To enable GraphQL and GraphiQL interface in your API, simply require
the graphql-php package using Composer:
composer require webonyx/graphql-php
open http://localhost:8080/api/graphql
Disable GraphiQL
Update api/config/packages/api_platform.yaml adding:
api_platform:
# ...
graphql:
graphiql:
enabled: false
# ...
Load resource using GraphQL
{
post (id:"/api/posts/1") {
id,
title,
body
}
}
Load resource using GraphQL form the CLI
curl -X POST 
-H "Content-Type: application/json" 
-d '{ "query": "{ post(id:"/api/posts/1") { id,
title, body }}" }' 
http://localhost:8080/api/graphql
Load resource relations using GraphQL
{
post (id:"/api/posts/1") {
title,
body,
type {
id,
name
}
}
}
Load resource relations using GraphQL form the CLI
curl -X POST 
-H "Content-Type: application/json" 
-d '{ "query": "{ post(id:"/api/posts/1") { id, title,
body, type { id, name } }}" }' 
http://localhost:8080/api/graphql
JWT
Authentication
Creating a modern web application using  Symfony API Platform Atlanta
JWT Dependencies
# JWT

composer require lexik/jwt-authentication-bundle
JWT Refresh

gesdinet/jwt-refresh-token-bundle
JWT Events (create)
# config/services.yaml

AppEventListenerJWTCreatedListener:

tags:

- {

name: kernel.event_listener, 

event: lexik_jwt_authentication.on_jwt_created,

method: onJWTCreated

}


# src/EventListener/JWTCreatedListener.php

public function onJWTCreated(JWTCreatedEvent $event)

{

$data = $event->getData();

$user = $event->getUser();

$data['organization'] = $user->getOrganization()->getId();

$event->setData($data);

}
JWT Events (success)
# config/services.yaml

AppEventListenerAuthenticationSuccessListener:

tags:

- {

name: kernel.event_listener, 

event: lexik_jwt_authentication.on_authentication_success,

method: onAuthenticationSuccessResponse

}

# src/EventListener/AuthenticationSuccessListener.php

public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event) 

{

$data = $event->getData();

$user = $event->getUser();

$data[‘roles'] = $user->getOrganization()->getRoles();

$event->setData($data);

}
React+Redux+Saga+
AntDesign
dvajs/dva - React and redux based framework. 
https://github.com/dvajs/dva
React / Redux / Saga / AntDesign
> Use webpack instead of roadhog.
> Use apollo-fetch for GraphQL calls.
> Use jwt-decode to interact with JWT.
> Use LocalStorage for simple key/values storage.
> Use IndexedDB for encrypted and/or more complex data structures.
> Use Socket-IO + Redis to sync & communicate between API & ReactJS.
Tips
Headless approach
Symfony API + Gatsby
● API / GraphQL
Blazing fast site generator for React
Creating a modern web application using  Symfony API Platform Atlanta
● Build● API / GraphQL
Must have plugins
• gatsby-source-graphql
• gatsby-transformer-remark
• gatsby-remark-images
• gatsby-remark-external-links
• gatsby-plugin-sharp
• gatsby-transformer-sharp
• gatsby-plugin-react-helmet
Choose the right tool for
the job
Thank you … Questions?
Feel free to ping me during the event,
at the parties, after-parties, and twitter @jmolivas

More Related Content

What's hot (20)

PDF
RESTful API development in Laravel 4 - Christopher Pecoraro
Christopher Pecoraro
 
PDF
Laravel 101
Commit University
 
PPTX
Workshop Laravel 5.2
Wahyu Rismawan
 
PDF
API Platform and Symfony: a Framework for API-driven Projects
Les-Tilleuls.coop
 
PDF
Bootstrat REST APIs with Laravel 5
Elena Kolevska
 
PDF
RESTful API Design & Implementation with CodeIgniter PHP Framework
Bo-Yi Wu
 
PPT
Building a p2 update site using Buckminster
guest5e2b6b
 
PPTX
Laravel 5
Sudip Simkhada
 
PDF
170517 damien gérard framework facebook
Geeks Anonymes
 
PDF
Hello World on Slim Framework 3.x
Ryan Szrama
 
ODP
Javascript laravel's friend
Bart Van Den Brande
 
PPT
Red5 - PHUG Workshops
Brendan Sera-Shriar
 
PPTX
Laravel Beginners Tutorial 2
Vikas Chauhan
 
PPTX
Laravel - Website Development in Php Framework.
SWAAM Tech
 
PPTX
Power Shell and Sharepoint 2013
Mohan Arumugam
 
PPT
Building Single Page Application (SPA) with Symfony2 and AngularJS
Antonio Peric-Mazar
 
PDF
The new features of PHP 7
Zend by Rogue Wave Software
 
PDF
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...
Edureka!
 
PDF
Rails 4.0
Robert Gogolok
 
PPTX
Webinar - Office 365 & PowerShell : A Match Made in Heaven
Sébastien Levert
 
RESTful API development in Laravel 4 - Christopher Pecoraro
Christopher Pecoraro
 
Laravel 101
Commit University
 
Workshop Laravel 5.2
Wahyu Rismawan
 
API Platform and Symfony: a Framework for API-driven Projects
Les-Tilleuls.coop
 
Bootstrat REST APIs with Laravel 5
Elena Kolevska
 
RESTful API Design & Implementation with CodeIgniter PHP Framework
Bo-Yi Wu
 
Building a p2 update site using Buckminster
guest5e2b6b
 
Laravel 5
Sudip Simkhada
 
170517 damien gérard framework facebook
Geeks Anonymes
 
Hello World on Slim Framework 3.x
Ryan Szrama
 
Javascript laravel's friend
Bart Van Den Brande
 
Red5 - PHUG Workshops
Brendan Sera-Shriar
 
Laravel Beginners Tutorial 2
Vikas Chauhan
 
Laravel - Website Development in Php Framework.
SWAAM Tech
 
Power Shell and Sharepoint 2013
Mohan Arumugam
 
Building Single Page Application (SPA) with Symfony2 and AngularJS
Antonio Peric-Mazar
 
The new features of PHP 7
Zend by Rogue Wave Software
 
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...
Edureka!
 
Rails 4.0
Robert Gogolok
 
Webinar - Office 365 & PowerShell : A Match Made in Heaven
Sébastien Levert
 

Similar to Creating a modern web application using Symfony API Platform Atlanta (20)

PDF
API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)
Les-Tilleuls.coop
 
PDF
Building APIs in an easy way using API Platform
Antonio Peric-Mazar
 
PPTX
Creating hypermedia APIs in a few minutes using the API Platform framework
Les-Tilleuls.coop
 
PDF
A high profile project with Symfony and API Platform: beIN SPORTS
Smile I.T is open
 
PDF
Using API platform to build ticketing system (translations, time zones, ...) ...
Antonio Peric-Mazar
 
PDF
API Platform: Full Stack Framework Resurrection
Les-Tilleuls.coop
 
PDF
Using API Platform to build ticketing system #symfonycon
Antonio Peric-Mazar
 
PDF
REST easy with API Platform
Antonio Peric-Mazar
 
PDF
High quality ap is with api platform
Nelson Kopliku
 
PDF
Build powerfull and smart web applications with Symfony2
Hugo Hamon
 
PPTX
API Platform: The Pragmatic API framework
soyuka1
 
KEY
The use of Symfony2 @ Overblog
Xavier Hausherr
 
PDF
Symfony components in the wild, PHPNW12
Jakub Zalas
 
PDF
Resting with OroCRM Webinar
Oro Inc.
 
PDF
The Naked Bundle - Symfony Barcelona
Matthias Noback
 
PDF
Алексей Веркеенко "Symfony2 & REST API"
Fwdays
 
PDF
Building APIs in an easy way using API Platform
Antonio Peric-Mazar
 
PDF
Symfony2 San Francisco Meetup 2009
Fabien Potencier
 
PDF
Drupal symfony
Tuz Valeriy
 
PPTX
A soa approximation on symfony
Carlos Agudo Belloso
 
API Platform 2.1: when Symfony meets ReactJS (Symfony Live 2017)
Les-Tilleuls.coop
 
Building APIs in an easy way using API Platform
Antonio Peric-Mazar
 
Creating hypermedia APIs in a few minutes using the API Platform framework
Les-Tilleuls.coop
 
A high profile project with Symfony and API Platform: beIN SPORTS
Smile I.T is open
 
Using API platform to build ticketing system (translations, time zones, ...) ...
Antonio Peric-Mazar
 
API Platform: Full Stack Framework Resurrection
Les-Tilleuls.coop
 
Using API Platform to build ticketing system #symfonycon
Antonio Peric-Mazar
 
REST easy with API Platform
Antonio Peric-Mazar
 
High quality ap is with api platform
Nelson Kopliku
 
Build powerfull and smart web applications with Symfony2
Hugo Hamon
 
API Platform: The Pragmatic API framework
soyuka1
 
The use of Symfony2 @ Overblog
Xavier Hausherr
 
Symfony components in the wild, PHPNW12
Jakub Zalas
 
Resting with OroCRM Webinar
Oro Inc.
 
The Naked Bundle - Symfony Barcelona
Matthias Noback
 
Алексей Веркеенко "Symfony2 & REST API"
Fwdays
 
Building APIs in an easy way using API Platform
Antonio Peric-Mazar
 
Symfony2 San Francisco Meetup 2009
Fabien Potencier
 
Drupal symfony
Tuz Valeriy
 
A soa approximation on symfony
Carlos Agudo Belloso
 
Ad

More from Jesus Manuel Olivas (20)

PDF
Remix & GraphQL: A match made in heaven with type-safety DX
Jesus Manuel Olivas
 
PDF
Drupal 10 Party GraphQL
Jesus Manuel Olivas
 
PDF
How to use Drupal to create editorial experiences your content creators will...
Jesus Manuel Olivas
 
PDF
Beyond Static: Building a Dynamic Application with Gatsby
Jesus Manuel Olivas
 
PDF
Drupal, GraphQL, Views, View Modes and Gatsby for a US Gov site CMS Philly
Jesus Manuel Olivas
 
PDF
Embracing the modern web using a Headless CMS with GatsbyJS CMS Philly
Jesus Manuel Olivas
 
PDF
Embracing the modern web using a Headless CMS with GatsbyJS Stanford
Jesus Manuel Olivas
 
PDF
Building a modern application using Symfony API Platform and GatsbyJS PHP QRO
Jesus Manuel Olivas
 
PDF
Building a dynamic application with GatsbyJS-Tec-Mexicali
Jesus Manuel Olivas
 
PDF
Building a modern web application in the cloud partnercon
Jesus Manuel Olivas
 
PDF
Embracing the modern web using Drupal as a Headless CMS with Gatsby BADCamp
Jesus Manuel Olivas
 
PDF
Blazing fast sites using Blaze, Hybrid CMS NYC
Jesus Manuel Olivas
 
PDF
Embracing the modern web using Drupal as Headless CMS with GatsbyJS NYC
Jesus Manuel Olivas
 
PDF
Writing a slack chatbot seattle
Jesus Manuel Olivas
 
PDF
Building a Modern Web Application in the Cloud TecNerd
Jesus Manuel Olivas
 
PDF
How to keep Drupal relevant in the Git-based and API-driven CMS era Florida
Jesus Manuel Olivas
 
PDF
How to keep Drupal relevant in the Git-based and API-driven CMS era DrupalCampNJ
Jesus Manuel Olivas
 
PDF
Tools and Projects Dec 2018 Edition
Jesus Manuel Olivas
 
PDF
How to keep Drupal relevant in the Git-based and API-driven CMS era - BADCamp
Jesus Manuel Olivas
 
PDF
Battle of the CMS DrupalCampLA
Jesus Manuel Olivas
 
Remix & GraphQL: A match made in heaven with type-safety DX
Jesus Manuel Olivas
 
Drupal 10 Party GraphQL
Jesus Manuel Olivas
 
How to use Drupal to create editorial experiences your content creators will...
Jesus Manuel Olivas
 
Beyond Static: Building a Dynamic Application with Gatsby
Jesus Manuel Olivas
 
Drupal, GraphQL, Views, View Modes and Gatsby for a US Gov site CMS Philly
Jesus Manuel Olivas
 
Embracing the modern web using a Headless CMS with GatsbyJS CMS Philly
Jesus Manuel Olivas
 
Embracing the modern web using a Headless CMS with GatsbyJS Stanford
Jesus Manuel Olivas
 
Building a modern application using Symfony API Platform and GatsbyJS PHP QRO
Jesus Manuel Olivas
 
Building a dynamic application with GatsbyJS-Tec-Mexicali
Jesus Manuel Olivas
 
Building a modern web application in the cloud partnercon
Jesus Manuel Olivas
 
Embracing the modern web using Drupal as a Headless CMS with Gatsby BADCamp
Jesus Manuel Olivas
 
Blazing fast sites using Blaze, Hybrid CMS NYC
Jesus Manuel Olivas
 
Embracing the modern web using Drupal as Headless CMS with GatsbyJS NYC
Jesus Manuel Olivas
 
Writing a slack chatbot seattle
Jesus Manuel Olivas
 
Building a Modern Web Application in the Cloud TecNerd
Jesus Manuel Olivas
 
How to keep Drupal relevant in the Git-based and API-driven CMS era Florida
Jesus Manuel Olivas
 
How to keep Drupal relevant in the Git-based and API-driven CMS era DrupalCampNJ
Jesus Manuel Olivas
 
Tools and Projects Dec 2018 Edition
Jesus Manuel Olivas
 
How to keep Drupal relevant in the Git-based and API-driven CMS era - BADCamp
Jesus Manuel Olivas
 
Battle of the CMS DrupalCampLA
Jesus Manuel Olivas
 
Ad

Recently uploaded (20)

PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 

Creating a modern web application using Symfony API Platform Atlanta

  • 1. Creating a modern web application using Symfony API Platform by Jesus Manuel Olivas / weKnow @jmolivas
  • 2. ● $ whoami Jesus Manuel Olivas [email protected] jmolivas jmolivas drupal.org/u/jmolivas jmolivas.weknowinc.com
  • 7. Drupal … Drupal … Drupal
  • 8. When all you have is a hammer, everything looks like a nail.
  • 11. Drupal Headless API + Front-end library ● Fetch
  • 12. What if … you do not need > Complex content model (paragraphs) > Editorial workflow > Revisions > GUI to manage Queries (views) > GUI to manage content model > Widgets and formatters
  • 13. Symfony API + Front-end library/framework ● API / GraphQL
  • 16. Symfony Flex … a Composer plugin for Symfony > Symfony Flex is a Composer plugin that modifies the behavior of the require, update, and remove composer commands. > Symfony Flex automates the most common tasks of Symfony applications, like installing and removing bundles and other dependencies using recipes defined in a manifest.json file.
  • 19. The API Platform Framework REST and GraphQL framework to build modern API-driven projects https://api-platform.com/
  • 20. Built on the Shoulders of Giants > Extend the framework with thousands of existing Symfony bundles and React components. > The API component includes the Symfony 4 flex, the Doctrine ORM. Client-side components and Admin based on React and a Docker configuration ready to startup your project using one single command. > Reuse all your Symfony, React and Docker skills and benefit of their high quality docs; you are in known territory.
  • 21. The API Platform Components API Schema Admin CRUD
  • 22. Try API-Platform # Clone code repository
 git clone https://github.com/api-platform/api-platform.git
  • 23. Recommendations and adjustments > Update route prefix at api/config/routes/api_platform.yaml file. api_platform:
 …
 prefix: /api > Update admin/.env and client/.env files (change protocol and port). REACT_APP_API_ENTRYPOINT=http://localhost:8080/api
  • 24. Start containers … and grab water, coffee, or a beer. # Start containers docker-compose up # Open browser open http://localhost/
  • 25. Add more formats Update api/config/packages/api_platform.yaml adding: formats: jsonld: [‘application/ld+json'] # first one is the default format json: ['application/json'] jsonhal: ['application/hal+json'] xml: ['application/xml', 'text/xml'] yaml: ['application/x-yaml'] csv: ['text/csv'] html: ['text/html']
  • 26. Remove default & add new entities > Remove default entity
 api/src/Entity/Greeting.php > Add new entities to api/src/Entity/ directory:
 console make:entity
  • 31. api/src/Entity/Post.php 1/3 <?php
 namespace AppEntity;
 
 use ApiPlatformCoreAnnotationApiResource;
 use DoctrineORMMapping as ORM;
 use SymfonyComponentValidatorConstraints as Assert; /**
 * @ApiResource
 * @ORMTable(name="post")
 * @ORMEntity
 */ class Post { … }
  • 32. api/src/Entity/Post.php 2/3 /**
 * @ORMId
 * @ORMGeneratedValue(strategy="AUTO")
 * @ORMColumn(type="integer")
 */
 private $id; /**
 * @ORMColumn
 * @AssertNotBlank
 */
 public $title = '';
 /**
 * @ORMColumn * @AssertNotBlank */ public $body = '';
  • 33. api/src/Entity/Post.php 3/3 /** * @ORMManyToOne(targetEntity="PostType") * @ORMJoinColumn(name="post_type_id", referencedColumnName="id", nullable=false) */ public $type; public function getId(): int { return $this->id; }
  • 34. Tracking Database changes # Add dependency composer require migrations # Execute command(s) console make:migration console doctrine:migrations:migrate
  • 35. Add FOSUserBundle # Add dependency composer require friendsofsymfony/user-bundle composer require symfony/swiftmailer-bundle https://symfony.com/doc/current/bundles/FOSUserBundle/index.html https://jolicode.com/blog/do-not-use-fosuserbundle
  • 36. Loading Posts using the Browser http://localhost:8080/api/posts http://localhost:8080/api/posts.json http://localhost:8080/api/posts.jsonld http://localhost:8080/api/posts/1 http://localhost:8080/api/posts/1.json
  • 37. Loading Posts using the CLI curl -X GET "http://localhost:8080/api/posts" -H "accept: application/json" curl -X GET "http://localhost:8080/api/posts/1" -H "accept: application/ld+json"
  • 38. ADD Posts from CLI curl -X POST "http://localhost:8080/api/posts" -H "accept: application/ld+json" -H "Content-Type: application/ld+json" -d '{ "title": "Post create from CLI", "body": "body- less", "type": "/api/post_types/1"}'
  • 39. UPDATE and REMOVE Posts from CLI curl -X PUT "http://localhost:8080/api/posts/9" -H "accept: application/ld+json" -H "Content-Type: application/ld+json" -d '{ "title": "Updated from CLI"}' curl -X DELETE "http://localhost:8080/api/posts/10" -H "accept: application/json"
  • 40. Serialization > API Platform allows to specify the which attributes of the resource are exposed during the normalization (read) and denormalization (write) process. It relies on the serialization (and deserialization) groups feature of the Symfony Serializer component. > In addition to groups, you can use any option supported by the Symfony Serializer such as enable_max_depth to limit the serialization depth.
  • 41. Serialization Relations (Post => PostType) 1/2 # api/src/Entity/Post.php & PostType.php * @ApiResource(attributes={ * "normalization_context"={"groups"={"read"}}, * "denormalization_context"={"groups"={"write"}} * })
  • 42. Serialization Relations (Post => PostType) 2/2 # Add use keyword to class use SymfonyComponentSerializerAnnotationGroups; # Add use keyword to properties * @Groups({"read"}) * @Groups({"read", "write"})
  • 43. GraphQL A query language for your API
  • 44. GraphQL GraphQL offers significantly more flexibility for integrators. Allows you to define in detail the only the data you want. GraphQL lets you replace multiple REST requests with a single call to fetch the data you specify.
  • 45. Add GraphQL To enable GraphQL and GraphiQL interface in your API, simply require the graphql-php package using Composer: composer require webonyx/graphql-php open http://localhost:8080/api/graphql
  • 46. Disable GraphiQL Update api/config/packages/api_platform.yaml adding: api_platform: # ... graphql: graphiql: enabled: false # ...
  • 47. Load resource using GraphQL { post (id:"/api/posts/1") { id, title, body } }
  • 48. Load resource using GraphQL form the CLI curl -X POST -H "Content-Type: application/json" -d '{ "query": "{ post(id:"/api/posts/1") { id, title, body }}" }' http://localhost:8080/api/graphql
  • 49. Load resource relations using GraphQL { post (id:"/api/posts/1") { title, body, type { id, name } } }
  • 50. Load resource relations using GraphQL form the CLI curl -X POST -H "Content-Type: application/json" -d '{ "query": "{ post(id:"/api/posts/1") { id, title, body, type { id, name } }}" }' http://localhost:8080/api/graphql
  • 53. JWT Dependencies # JWT
 composer require lexik/jwt-authentication-bundle JWT Refresh
 gesdinet/jwt-refresh-token-bundle
  • 54. JWT Events (create) # config/services.yaml
 AppEventListenerJWTCreatedListener:
 tags:
 - {
 name: kernel.event_listener, 
 event: lexik_jwt_authentication.on_jwt_created,
 method: onJWTCreated
 } 
 # src/EventListener/JWTCreatedListener.php
 public function onJWTCreated(JWTCreatedEvent $event)
 {
 $data = $event->getData();
 $user = $event->getUser();
 $data['organization'] = $user->getOrganization()->getId();
 $event->setData($data);
 }
  • 55. JWT Events (success) # config/services.yaml
 AppEventListenerAuthenticationSuccessListener:
 tags:
 - {
 name: kernel.event_listener, 
 event: lexik_jwt_authentication.on_authentication_success,
 method: onAuthenticationSuccessResponse
 }
 # src/EventListener/AuthenticationSuccessListener.php
 public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event) 
 {
 $data = $event->getData();
 $user = $event->getUser();
 $data[‘roles'] = $user->getOrganization()->getRoles();
 $event->setData($data);
 }
  • 57. dvajs/dva - React and redux based framework.  https://github.com/dvajs/dva
  • 58. React / Redux / Saga / AntDesign
  • 59. > Use webpack instead of roadhog. > Use apollo-fetch for GraphQL calls. > Use jwt-decode to interact with JWT. > Use LocalStorage for simple key/values storage. > Use IndexedDB for encrypted and/or more complex data structures. > Use Socket-IO + Redis to sync & communicate between API & ReactJS. Tips
  • 61. Symfony API + Gatsby ● API / GraphQL
  • 62. Blazing fast site generator for React
  • 64. ● Build● API / GraphQL
  • 65. Must have plugins • gatsby-source-graphql • gatsby-transformer-remark • gatsby-remark-images • gatsby-remark-external-links • gatsby-plugin-sharp • gatsby-transformer-sharp • gatsby-plugin-react-helmet
  • 66. Choose the right tool for the job
  • 67. Thank you … Questions? Feel free to ping me during the event, at the parties, after-parties, and twitter @jmolivas