Upgrade to Pro — share decks privately, control downloads, hide ads and more …

API Platform 2.6: PHP 8 support, Next.js and Nu...

API Platform 2.6: PHP 8 support, Next.js and Nuxt.js app generator, Caddy server, ActivityPub and much more!

Avatar for Kévin Dunglas

Kévin Dunglas

December 03, 2020
Tweet

More Decks by Kévin Dunglas

Other Decks in Programming

Transcript

  1. @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
  2. @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
  3. @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; } }
  4. 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
  5. @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; } }
  6. @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; } }
  7. @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 …
  8. @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)
  9. @dunglas Automatic Persistenc e (maybe NOT what you need) /**

    * @ApiResource * @ORM\Entity */ class Book { /* … */ }
  10. @dunglas PHP 8 Attributes use ApiPlatform\Core\Annotation\ApiResource; #[ApiResource] class Book {

    public function __construct( public string $id, public string $title, ) {} }
  11. @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!
  12. @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 ❤
  13. @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
  14. @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)
  15. @dunglas Pre-existing Generators TypeScript model definition s React + Redux

    (no SSR ) Vue.js (no SSR ) React Nativ e Quasa r Vuetify
  16. @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…
  17. @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
  18. 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
  19. @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
  20. @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
  21. @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
  22. @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!
  23. @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
  24. @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)…
  25. @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:
  26. @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