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

API platform and how to use it

API platform and how to use it

A brief introduction to API platform (Symfony) for 4xxi developers.

Evgeny Smirnov

May 30, 2022
Tweet

More Decks by Evgeny Smirnov

Other Decks in Programming

Transcript

  1. +

  2. +

  3. + Out of the box “CRUD, data validation, pagination, fi

    ltering, sorting, json/hydra, GraphQL, swagger, CORS, OWASP inside…”
  4. + Getting started 1. Official “Getting started” guide 2. SymfonyCast:

    RESTful APIs and 
 API Platform guides 3. StackOverflow
  5. +

  6. + final class UserQuizDataPersister implements ContextAwareDataPersisterInterface { private $decorated; private

    $security; … public function persist($data, array $context = []) { if (is_null($data->getUser())) { $user = $this->security->getUser(); $data->setUser($user); } $result = $this->decorated->persist($data, $context); return $result; } public function remove($data, array $context = []) { return $this->decorated->remove($data, $context); } } Data Persisters
  7. + #[AsController] class SkipUserQuestion extends AbstractController { public function __invoke(UserQuestion

    $data): UserQuestion { $data->setStatus(UserQuestion::STATUS_SKIPPED); return $data; } } Pseudo Controllers
  8. + public function transform($data, string $to, array $context = [])

    { $resetPasswordRequest = new ResetPasswordRequest(); $user = $this->userRepository->findOneByEmail($data->getEmail()); $resetPasswordRequest->setUser($user); $now = new \DateTimeImmutable(); $expiredAt = new \DateTimeImmutable('+1 hour'); $resetPasswordRequest->setRequestedAt($now); $resetPasswordRequest->setExpiresAt($expiredAt); return $resetPasswordRequest; } Data Transformers
  9. + final class ResetPasswordRequestInput { #[Groups(['resetPasswordRequest:create', 'resetPasswordRequest:read'])] #[Assert\NotBlank(groups: ['validation:create'])] #[Assert\Email()]

    private $email; public function getEmail(): ?string { return $this->email; } public function setEmail(string $email): self { $this->email = $email; return $this; } } DTOs
  10. + Useful add ons ✅ JWT through LexikJWTAuthenticationBundle ✅ JWT

    refresh tokens GesdinetJWTRefreshTokenBundle ❌ Complete sign up / sign in ❌ Role based API versions
  11. + 4-5 extra classes (DTOs, Transformers, etc.) or Custom controller

    outside of API Platform* * and extra classes for OpenAPI docs…
  12. +

  13. +

  14. +