Slide 1

Slide 1 text

API PLATFORM CON 2023 - LILLE, FRANCE & ONLINE API PLATFORM 4 API PLATFORM CON 2024 - LILLE, FRANCE & ONLINE

Slide 2

Slide 2 text

About Me ✔ API Platform creator ✔ Les-Tilleuls.coop co-founder ✔ Polyglot developer Kévin Dunglas [email protected] @dunglas dunglas.dev

Slide 3

Slide 3 text

API Platform Community ✔ 700+ conference attendees ✔ 8,442 stargazers on GitHub ✔ 864 code and docs contributors ✔ 5,505 people on Slack

Slide 4

Slide 4 text

Crossing the Atlantic

Slide 5

Slide 5 text

Getting Started… with a New Stack composer create-project laravel/laravel my-api cd my-api # or open an existing Laravel project!

Slide 6

Slide 6 text

Laravel In Numbers ✔ Most popular web framework (across all languages) ✔ 78,215 stargazers on GitHub ✔ 3452 contributors ✔ 40,432 users on Discord ✔ Just raised a $57 million Series A from Accel

Slide 7

Slide 7 text

Installing API Platform for Laravel 🎉 composer require api-platform/laravel

Slide 8

Slide 8 text

Disclaimer: This Guy Wrote Most of the Code

Slide 9

Slide 9 text

Start the Dev Web Server php artisan serve

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Wasn’t API Platform Symfony-specific? ✔ Since v2: a standalone PHP library… ✔ …with two official framework integrations: ● a Symfony bundle from day one ● a Laravel package as of today ✔ Symfony and Laravel are 1st class citizens ✔ Both run the same code: one code base, monolithic repository with read-only subtree split ✔ Most features (~90%) are available for both frameworks

Slide 12

Slide 12 text

It’s Even Possible To Use API Platform Without a Framework

Slide 13

Slide 13 text

What’s Inside the Laravel Package? ✔ Provider: registers services provided by API Platform in Laravel Dependency Injection Container ✔ Routing: automatically registers and implements REST routes ✔ Docs: automatically generates OpenAPI (formerly known as Swagger) spec ✔ GraphQL: state-of-the-art GraphQL support implementing the Relay spec ✔ Idiomatic integrations with the Laravel ecosystem: ● Eloquent, Validation, Gates, Policies, Octane, Broadcast, Pest…

Slide 14

Slide 14 text

Create an Eloquent Model: php artisan make:model Book namespace App\Models; use Illuminate\Database\Eloquent\Model; class Book extends Model {}

Slide 15

Slide 15 text

Eloquent ✔ Eloquent is the Laravel ORM ✔ It implements the Active Record pattern ✔ Table columns are automatically exposed as magic model class properties ✔ It can also be used as a standalone library, even in Symfony with wouterj/eloquent-bundle

Slide 16

Slide 16 text

Eloquent (ctd) ✔ Eloquent supports SQLite, PostgreSQL, MySQL, MariaDB and SQL Server ✔ All you have to do is to create a table (books in our example) and add columns, no code and no mapping required! ✔ You can do it manually or use migrations (recommended)

Slide 17

Slide 17 text

Create a Migration: php artisan make:migration Schema::create('books', function (Blueprint $table) { $table->id(); $table->string('isbn')->nullable(); $table->string('title'); $table->text('description'); $table->string('author'); $table->date('publication_date')->nullable(); $table->timestamps(); });

Slide 18

Slide 18 text

Run the Migration: php artisan migrate

Slide 19

Slide 19 text

Your State-of-the-Art API With One Attribute namespace App\Models; use ApiPlatform\Metadata\ApiResource; use Illuminate\Database\Eloquent\Model; #[ApiResource] class Book extends Model {}

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

✔ API Platform introspects the database (column names, types, constraints…) to populate metadata ✔ Serialization, OpenAPI and Hydra docs… are generated from these metadata ✔ The same attributes as with the Symfony variant are used ✔ Everything is entirely configurable and extensible Under the Hood

Slide 23

Slide 23 text

By default, API Platform: ✔ exposes a true REST API (HATEOAS/hypermedia) ✔ follows standards as closely as possible: ● W3C’s JSON-LD/RDF with Schema.org, Hydra (interoperability, AI, big data) ● IETF’s RFC 7807 (API errors), 7386 (JSON Merge Patch), 8594 (endpoint deprecations), etc ✔ embraces OWASP REST security best practices Unrivalled Compliance With Open Standards

Slide 24

Slide 24 text

Other Formats: JSON:API, HAL and CSV (YAML is planned, PR welcome!) // config/api-platform.php return [ 'formats' => [ 'jsonld' => ['application/ld+json'], 'jsonapi' => ['application/vnd.api+json'], 'jsonhal' => ['application/hal+json'], 'csv' => ['text/csv'], ], 'docs_formats' => [ 'jsonld' => ['application/ld+json'], 'jsonapi' => ['application/vnd.api+json'], 'jsonopenapi' => ['application/vnd.openapi+json'], 'html' => ['text/html'], ], // ...

Slide 25

Slide 25 text

GraphQL? Challenge Accepted! composer require api-platform/graphql // config/api-platform.php 'graphql' => [ 'enabled' => true, // ...

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Hiding Fields, The Laravel Way namespace App\Models; use ApiPlatform\Metadata\ApiResource; use Illuminate\Database\Eloquent\Model; #[ApiResource] class Book extends Model { //The attributes that should be hidden (deny list). protected $hidden = ['isbn']; }

Slide 28

Slide 28 text

Or Using an Allow List namespace App\Models; use ApiPlatform\Metadata\ApiResource; use Illuminate\Database\Eloquent\Model; #[ApiResource] class Book extends Model { // The attributes that should be visible (allow list). protected $visible = ['title', 'description']; }

Slide 29

Slide 29 text

Adding Filters namespace App\Models; use ApiPlatform\Laravel\Eloquent\Filter\PartialSearchFilter; use ApiPlatform\Metadata\{ApiResource, QueryParameter}; use Illuminate\Database\Eloquent\Model; #[ApiResource] #[QueryParameter( key: 'title', filter: PartialSearchFilter::class, )] class Book extends Model {}

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

✔ Text: equals, partial matching, start, end ✔ Date: equals, greater, lower… ✔ Boolean logic: and, or ✔ Ordering ✔ Range ✔ Vulcain/GraphQL-like “properties” filter Filter values are also validated! Built-in Filters

Slide 32

Slide 32 text

✔ API Platform comes with a lot of settings ✔ Almost everything is configurable ✔ You can configure behavior: ● Globally, in config/api-platform.yaml ● For an endpoint, using #[ApiResource] ● For an operation (e.g. #[Post]), using the operation attributes Configuring the API

Slide 33

Slide 33 text

Config Example #[ApiResource( paginationItemsPerPage: 10, operations: [ // Read-only endpoint new GetCollection(), new Get(), ], )] class Book extends Model {}

Slide 34

Slide 34 text

API Platform hooks into the native authentication mechanism, built-in support for: ✔ Standard Laravel Users ✔ Laravel Sanctum: SPA and simple API token ✔ Laravel Passport: OAuth server ✔ Laravel Socialite: OAuth Client (Facebook, X, LinkedIn, Google, GitHub, GitLab, Bitbucket, and Slack) Authentication

Slide 35

Slide 35 text

Data Validation php artisan make:request BookFormRequest namespace App\Http\Requests; class BookFormRequest extends \Illuminate\Foundation\Http\FormRequest { public function authorize(): bool { return $this->user()->isAdmin(); } public function rules(): array { return [ 'title' => 'required|unique:books|max:255', 'description' => 'required', 'author' => 'required|max:255', ]; } }

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

Authorization Logic php artisan make:policy BookPolicy --model=Book namespace App\Policies; use App\Models\Book; use App\Models\User; class BookPolicy { public function viewAny(User $user): bool {} public function view(User $user, Book $book): bool {} public function create(User $user): bool {} public function update(User $user, Book $book): bool {} public function delete(User $user, Book $book): bool {} public function restore(User $user, Book $book): bool {} public function forceDelete(User $user, Book $book): bool {} }

Slide 38

Slide 38 text

FrankenPHP composer require laravel/octane php artisan octane:install --server=frankenphp php artisan octane:frankenphp

Slide 39

Slide 39 text

The Laravel variant is compatible with all API Platform JavaScript tools: ✔ Create Client: Next.js, Nuxt, Angular, Quasar and Vuetify PWA scaffolding ✔ Admin: React-Admin based admin UI Frontend

Slide 40

Slide 40 text

✔ Mercure v1.0 ✔ Native Laravel Broadcast integration ✔ Native Laravel Echo integration Coming Soon: Real-time

Slide 41

Slide 41 text

We're so excited that API Platform now supports Laravel. It's never been easier to create best-practice APIs using Laravel when paired with API Platform! TAYLOR OTWELL AND THE LARAVEL TEAM

Slide 42

Slide 42 text

Discover the new API Platform documentation api-platform.com made with by

Slide 43

Slide 43 text

FOLLOW ME! @dunglas dunglas.dev / les-tilleuls.coop / api-platform.com Thank you! Any questions?