Share
Embed
API Platform 2.6: PHP 8 support, Next.js and Nuxt.js app generator, Caddy server, ActivityPub and much more!
Kévin Dunglas
December 03, 2020
Transcript
60 slides
-
@dunglas API Platform: key points A rock-solid API-first project in
minutes State-of-the-art API technologies: hypermedia, Mercure, Vulcain, GraphQL … Batteries included: create (server), consume (clients) and deploy Customizable, extensible, modular -
@dunglas Design AND Code First Design the public data model
exposed by your AP I Describe it as a set of Data Transfer Objects (DTO ) Add types Embed human-readable docs -
@dunglas Design AND Code First namespace App\Entity; use ApiPlatform\Core\Annotation\ApiResource; /**
* A book * * @ApiResource */ class Book { /** Identifier of the book */ public string $id; /** Title of the book */ public string $title; public function __construct(string $id, string $title) { $this->id = $id; $this->title = $title; } } -
Machine-readable API docs are dynamically generated from PHP class metadata
: ❏ Hydra / RD F ❏ OpenAPI v2 (aka Swagger) and v 3 ❏ GraphQL schem a ❏ JSON Schema -
@dunglas Plug your domain: read class BookDataProvider implements /* Item|Collection|…
*/DataProviderInterface { public function getCollection(string $resourceClass /*…*/): iterable { // Return the collection of Book objects (can be a paged collection) // matching the passed criteria } public function getItem(string $resourceClass, $id /*…*/): Book { // Return the Book object having this ID } public function supports(string $resourceClass /*…*/): bool { // Register as many data providers as needed return $resourceClass === Book::class; } } -
@dunglas Plug your domain: write class BookDataPersister implements /*…*/DataPersisterInterface {
public function persist($data /*…*/) { // Extract the data from the DTO and use it } public function remove($data /*…*/) { // Remove this resource } public function supports($data /*…*/): bool { // Plug as many data persisters as needed return $data instanceof Book; } } -
@dunglas Enjoy the Built-in Features! Aut h Validatio n Paginatio
n Filterin g Sortin g Real-time Push: Mercur e Rels preloading: Vulcai n Docs: Swagger UI, Redoc, GraphiQL… Automated test s Hypermedi a HTTP/ 2 RDF / JSON-LD / Hydr a GraphQ L JSON:AP I HA L JSON Schem a … -
@dunglas Available Integrations Core: a standalone PHP lib built on
top of Symfony Component s Symfony Bundle: Symfony Framework integratio n Doctrine Bridge: automatic persistence with Doctrine ORM (read/write ) MongoDB Bridge: automatic persistence with Doctrine MongoDB ODM (read/write ) Elasticsearch Bridge: ES integration (read-only) -
@dunglas Automatic Persistenc e (maybe NOT what you need) /**
* @ApiResource * @ORM\Entity */ class Book { /* … */ } -
@dunglas PHP 8 Attributes use ApiPlatform\Core\Annotation\ApiResource; #[ApiResource] class Book {
public function __construct( public string $id, public string $title, ) {} } -
@dunglas Global Defaults Contributed by Benoît Polaszek ❤ # config/packages/api_platform.yaml
api_platform: defaults: itemOperations: ["get"] collectionOperations: ["get"] mercure: true # This config is applied to all operations of all resources! -
@dunglas Access Control For Properties use ApiPlatform\Core\Annotation\ApiProperty; class Book {
#[\ApiProperty(security: "is_granted('ROLE_ADMIN')"] public $adminOnlyProperty; // Only admins will see the adminOnlyProperty prop } Contributed by Frédéric Barthelet & Grégoire Hébert ❤ -
@dunglas More New Features Full PHP 8 compatibility ❤ Pierre
du Plessis, Antoine Bluchet & me GraphQL subscriptions (using Mercure) ❤ Alan Poulain Mercure support for MongoDB ❤ Alan Poulain Support for @Ignore & @SerializedName Serializer annotations ❤ Alexander Janssen, Marcel Malberg & MartkCz Brand new OpenAPI docs builder ❤ Antoine Bluchet Better support for composed identifiers ❤ Antoine Bluchet Improved Vulcain support ❤ Pierre Thibaudeau and many others -
@dunglas API Platform Client Generator Scaffolds client apps by parsing
API doc s Supports Hydra (preferred) and OpenAPI (experimental ) Pluggable skeletons: bring your own ! Works with any APIs (even if not written in PHP) -
@dunglas Pre-existing Generators TypeScript model definition s React + Redux
(no SSR ) Vue.js (no SSR ) React Nativ e Quasa r Vuetify -
@dunglas New Generator: Contributed by Grégory Copin ❤ Replace the
old React/Redux generator in the distribution ! No config needed, TypeScrip t Server-Side Renderin g Smart bundling, route prefetching… -
@dunglas New Generator: Contributed by Alain Hippolyte ❤ Easy to
learn (good old HTML/JS/CSS ) Server-Side Renderin g Performant $ docker-compose exec client \ generate-api-platform-client \ —-generator nuxt -
It’s the magic of REST’s layer system, but this can
be hard to understand, and hard to manage in prod. + Varnish Cache in pro d -
@dunglas Caddy Web Server Free and Open Source web serve
r Super fast, written in G o Extensibl e Native PHP FPM support Automatic HTTPS, even for localhos t HTTP/3 ! Cloud native (API, hot config reloading… ) Can be used as a Kubernetes Ingress -
@dunglas One Daemon To Rule Them All Caddy had almost
everything needed by most Symfony / API Platform projects except : a Mercure module (async APIs, push ) a Vulcain module (RESTful relations preloading ) an HTTP cache module -
@dunglas Docker Services in APIP 2.6 PHP FP M Caddy
server (with all modules ) Postgres (optional ) Node / NextJS (optional ) client and admin containers have been merged in a single React app -
@dunglas Introducing Symfony Docker Same Caddy-based setu p But only
PHP and Symfony ! No Postgres, no Node -
@dunglas New: Symfony Flex’s Docker Configurator $ docker-compose exec php
\ composer req orm Doctrine, a Postgres service and the pdo_pgsql driver are installed! $ docker-compose exec php \ composer req api API Platform (PHP only) is installed! -
@dunglas I Don’t Want Docker At All! A version of
Caddy, including the same modules as in Symfony's Docker is available as a standalone binary . You can also build your own binary and include only the modules you want: $ xcaddy build \ —-with github.com/dunglas/mercure/caddy \ —-with github.com/dunglas/vulcain/caddy -
@dunglas The ActivityPub Protocol A decentralized social networking protoco l
W3C Recommendatio n Servers are interoperable and can be federated: the fediverse Implemented by Mastodon (Twitter-like), PeerTube (YouTube-like), PixelFed (Instagram- like), Mobilizon (Meetup-like)… -
@dunglas Community API Platform exists thanks to its communit y
590 contributors ❤ code, docs, community support, propaganda (aka evangelization) … 6,3K GitHub stars, 5M+ download s ~30 repositories written in PHP, JS, Go, Shell … An awesome sponsor: -
@dunglas Next Release: API Platform 3 Upgrade path: API Platform
3 = API Platform 2.7 minus the backward compatibility layer (same as Symfony ) PHP 8 only ! Revamped subresources, with write support Revamped UX