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

PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / p...

PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform

Takashi Kanemoto

December 22, 2024
Tweet

More Decks by Takashi Kanemoto

Other Decks in Programming

Transcript

  1. / 91 PHP API Platform Web API 2 024 /

    12 / 2 2 PHP 2024 @ttskch  1 #track 4 入門 #phpcon
  2. / 91 Web API PHP 1 行 API API REST

    GraphQL Symfony API Platform ...  16
  3. / 91 Web API PHP 1 行 API API REST

    GraphQL Symfony 201 5 / 09 2 024 / 0 8 Symfony Laravel ⾒ 20 24 / 09 API Platform ...  17
  4. / 91 Web API PHP 1 行 API API REST

    GraphQL Symfony 201 5 / 09 2 024 / 0 8 Symfony Laravel ⾒ 20 24 / 09 API Platform ...  19
  5. / 91 Web API PHP 1 行 API API REST

    GraphQL Symfony 201 5 / 09 2 024 / 0 8 Symfony Laravel ⾒ 20 24 / 09 API Platform ...  20
  6. / 91 Symfony 用 ⾒ Symfony API Platform Core Laravel

    用 ⾒ Laravel API Platform Core 方  25 用 ⾒ https://api-platform.com/docs/core/bootstrap/
  7. / 91 API Platform Core  26 https://github.com/api-platform/api-platform ⭐ 8

    . 7 k https://github.com/api-platform/core ⭐ 2 . 5 k https://github.com/api-platform/admin ⭐ 4 87 https://github.com/api-platform/create-client ⭐ 374 https://github.com/api-platform/symfony ⭐ 8 https://github.com/api-platform/laravel ⭐ 49 _ …
  8. / 91 API Platform Core  27 https://github.com/api-platform/api-platform ⭐ 8

    . 7 k https://github.com/api-platform/core ⭐ 2 . 5 k https://github.com/api-platform/admin ⭐ 4 87 https://github.com/api-platform/create-client ⭐ 374 https://github.com/api-platform/symfony ⭐ 8 https://github.com/api-platform/laravel ⭐ 49 _ … API Platform Core API Platform
  9. / 91 API Platform Core  28 https://github.com/api-platform/api-platform ⭐ 8

    . 7 k https://github.com/api-platform/core ⭐ 2 . 5 k https://github.com/api-platform/admin ⭐ 4 87 https://github.com/api-platform/create-client ⭐ 374 https://github.com/api-platform/symfony ⭐ 8 https://github.com/api-platform/laravel ⭐ 49 _ … API Platform Core Subtree Split API Platform Core
  10. / 91 API Platform Core  29 https://github.com/api-platform/api-platform ⭐ 8

    . 7 k https://github.com/api-platform/core ⭐ 2 . 5 k https://github.com/api-platform/admin ⭐ 4 87 https://github.com/api-platform/create-client ⭐ 374 https://github.com/api-platform/symfony ⭐ 8 https://github.com/api-platform/laravel ⭐ 49 _ …
  11. / 91 API Platform Core  30 https://github.com/api-platform/api-platform ⭐ 8

    . 7 k https://github.com/api-platform/core ⭐ 2 . 5 k https://github.com/api-platform/admin ⭐ 4 87 https://github.com/api-platform/create-client ⭐ 374 https://github.com/api-platform/symfony ⭐ 8 https://github.com/api-platform/laravel ⭐ 49 _ … Core Symfony Admin Create Client 入
  12. / 91 API Platform Core  31 https://github.com/api-platform/api-platform ⭐ 8

    . 7 k https://github.com/api-platform/core ⭐ 2 . 5 k https://github.com/api-platform/admin ⭐ 4 87 https://github.com/api-platform/create-client ⭐ 374 https://github.com/api-platform/symfony ⭐ 8 https://github.com/api-platform/laravel ⭐ 49 _ …
  13. / 91 API Platform Core  32 https://github.com/api-platform/api-platform ⭐ 8

    . 7 k https://github.com/api-platform/core ⭐ 2 . 5 k https://github.com/api-platform/admin ⭐ 4 87 https://github.com/api-platform/create-client ⭐ 374 https://github.com/api-platform/symfony ⭐ 8 https://github.com/api-platform/laravel ⭐ 49 _ … Issue 子 🙆
  14. is

  15. / 91 色 入 身  35 API Platform Core

    入 Symfony api-platform/ symphony React Admin 面 api-platform/ admin Next.js 自 生 api-platform/ create-client Mercure 用 - Docker - Kubernetes Helm -
  16. / 91 色 入 身  36 API Platform Core

    入 Symfony api-platform/ symphony React Admin 面 api-platform/ admin Next.js 自 生 api-platform/ create-client Mercure 用 - Docker - Kubernetes Helm - 色 入 🫠
  17. / 91 API Platform Easy  39 入 自 API

    Platform Core 1 行 API 生 Easy
  18. / 91 API Platform Easy  40 入 自 API

    Platform Core 1 行 API 生 Easy
  19. / 91 API Platform "Easy Simple "  45 Simple

    1 小 士 目 Easy 用 Symfony Kévin Dunglas Symfony
  20. / 91 自己 2 API Platform 8 3 3 OpenAPI

    3 1 5  50 🔖 10 目
  21. / 91 API Platform Core API Platform Core  52

    $ composer require api-platform/laravel $ php artisan api-platform:install
  22. / 91 Article  55 $ php artisan make:model Article

    $ php artisan make:migration create_articles_table
  23. / 91  56 public function up(): void { Schema::create('articles',

    function (Blueprint $table) { $table->id(); + $table->string('title'); + $table->text('content')->nullable(); + $table->boolean('published')->default(false); $table->timestamps(); }); }
  24. / 91 API  58 <?php namespace App\Models; + use

    ApiPlatform\Metadata\ApiResource; use Illuminate\Database\Eloquent\Model; + #[ApiResource] class Article extends Model { // }
  25. / 91 自己 2 API Platform 8 3 3 OpenAPI

    3 1 5  64 🔖 13 目
  26. / 91 api-platform/laravel Eloquent 自 自 API State Provider /

    State Processor  69 State Provider State Processor 行
  27. / 91 namespace App\State; use ApiPlatform\Metadata\Operation; use ApiPlatform\State\ProviderInterface; final class

    ArticleProvider implements ProviderInterface { public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null { $article = /* ͜͜ʹهࣄΛऔಘ͢ΔॲཧΛॻ͘ */; return $article; } }  70 State Provider 自
  28. / 91 namespace App\State; use ApiPlatform\Metadata\Operation; use ApiPlatform\State\ProcessorInterface; class ArticleProcessor

    implements ProcessorInterface { public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed { /* ͜͜ʹهࣄΛӬଓԽʢ$data Λߋ৽ͯ͠ $data->save() ͳͲʣ͢ΔॲཧΛॻ͘ */ return $data; } }  71 State Processor 自
  29. / 91 namespace App\Providers; use ApiPlatform\State\ProcessorInterface; use App\State\ArticleProcessor; use App\State\ArticleProvider;

    use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { public function register(): void { $this->app->tag([ArticleProvider::class], ProviderInterface::class); $this->app->tag([ArticleProcessor::class], ProcessorInterface::class); } public function boot(): void { } }  72 Laravel ⾒
  30. / 91 namespace App\Models; use ApiPlatform\Metadata\ApiResource; + use App\State\ArticleProcessor; +

    use App\State\ArticleProvider; use Illuminate\Database\Eloquent\Model; - #[ApiResource] + #[ApiResource( + provider: ArticleProvider::class, + processor: ArticleProcessor::class, + )] class Article extends Model { // }  73 API
  31. / 91 自己 2 API Platform 8 3 3 OpenAPI

    3 1 5  74 🔖 16 目
  32. / 91 OpenAPI  75 Web API 言 OpenAPI OpenAPI

    JSON YAML OpenAPI Swagger UI API OpenAPI TypeScript 言 API 自 生
  33. / 91 API Platform OpenAPI  76 API Platform OpenAPI

    自 生 手 OpenAPI API 自 生 力
  34. / 91  77 API Platform 自 OpenAPI 力 Laravel

    DB SQLite bool OpenAPI boolean string 🤦
  35. / 91 namespace App\Models; + use ApiPlatform\Metadata\ApiProperty; use ApiPlatform\Metadata\ApiResource; use

    App\State\ArticleProcessor; use App\State\ArticleProvider; use Illuminate\Database\Eloquent\Model; #[ApiResource( provider: ArticleProvider::class, processor: ArticleProcessor::class, )] + #[ApiProperty(schema: ['type' => 'boolean'], property: 'published')] class Article extends Model { // }  80