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

Zero to API With Lumen

Zero to API With Lumen

Ever wanted to create an API but weren't sure how to get started? Create APIs quickly with the Lumen PHP micro-framework.

Amanda Folson

July 28, 2016
Tweet

More Decks by Amanda Folson

Other Decks in Programming

Transcript

  1. Amanda Folson - Laracon 2016
    @AmbassadorAwsum
    Zero to API with
    Lumen

    View Slide

  2. Who Am I?
    ▸ Amanda Folson
    ▸ Developer Advocate at
    GitLab
    ▸ Professional
    Conference Attendee
    ▸ OSS ~10 years
    ○ Many years of APIs

    View Slide

  3. About Lumen
    ▸ Microframework
    ▸ Circa April 2015
    ▸ Trimmed down Laravel
    ▸ Laravel upgrade path

    View Slide

  4. Why Lumen?

    View Slide

  5. Monolithic->Microservices

    View Slide

  6. Blacklist
    Build Zones Honeypot
    API
    Scheduler
    Thing Restarter

    View Slide

  7. What the heck is an API?

    View Slide

  8. REST APIs
    ▸ Representational State Transfer
    ▸ Can return XML, JSON, YAML, Markdown
    ▸ RAPIDLY gaining adoption over SOAP/XML-RPC
    ○ No need to know order of parameters

    View Slide

  9. HTTP-based APIs
    ▸ Often HTTP-based
    ○ Many things
    natively speak
    HTTP
    ▸ Stateless
    ○ No sessions!
    ○ HATEOAS

    View Slide

  10. HTTP Methods
    OPTIONS
    GET
    DELETE
    PATC
    H
    PUT
    POST

    View Slide

  11. Coming From Laravel

    View Slide

  12. Getting Started

    View Slide

  13. DotEnv

    View Slide

  14. Facades + Eloquent

    View Slide

  15. Routing
    Laravel Routing
    Lumen Routing

    View Slide

  16. Artisan

    View Slide

  17. Differences Between Laravel and Lumen in Artisan
    Lumen’s `artisan`:
    ▸ No `serve` command
    ▸ No `key:generate`
    ▸ No `tinker`
    ▸ No `env`
    ▸ No `down`
    ▸ No `vendor:publish`

    View Slide

  18. Migrations
    ▸ Install
    ▸ Refresh
    ▸ Reset
    ▸ Rollback
    ▸ Status
    ▸ `db:seed`

    View Slide

  19. Make Migrations

    View Slide

  20. Differences in Artisan’s Make Command
    Lumen’s `make`:
    ▸ ONLY has `make:migration`
    ▸ No `make:model`
    ▸ No `make:controller`
    ▸ No `make:auth` + scaffolding
    ▸ Many more since Lumen is slimmed down

    View Slide

  21. Pokémon API

    View Slide

  22. Resources
    ▸ /resource is a gateway to an area of an application
    ○ In this case, we hit /pokemon
    ○ We can get a single Pokémon or a collection of them

    View Slide

  23. Resource Collections

    View Slide

  24. Pokédex Routes

    View Slide

  25. A Note About Views...

    View Slide

  26. JSON

    View Slide

  27. Fractal

    View Slide

  28. Pokémon Controller - Index and Store

    View Slide

  29. Pokémon Controller - Show

    View Slide

  30. Pokémon Controller - Update

    View Slide

  31. Pokémon Controller - Destroy

    View Slide

  32. Pokémon Model

    View Slide

  33. Authentication
    ▸ AuthServiceProvider
    provided
    ○ But needs to be
    uncommented in
    app.php
    ▸ PLENTY of other
    options
    ○ JWT

    View Slide

  34. Using Authentication Middleware
    ▸ Many packages available (JWT, OAUTH2, Stormpath, etc.)
    ▸ JWT used below:

    View Slide

  35. A Note About Authentication...

    View Slide

  36. Testing

    View Slide

  37. Beyond This Talk
    ▸ HATEOAS and Hypermedia
    ▸ Rate limiting
    ▸ Running APIs at scale
    ▸ Events
    ▸ Queues
    ▸ Caching
    ▸ Encryption
    ▸ Logging
    ▸ Deploying

    View Slide

  38. /resources
    ▸ Lumen and Laravel docs
    ○ https://lumen.laravel.com/docs/
    ▸ Paul Redmond’s Book
    ○ https://www.apress.com/9781484221860
    ▸ Build APIs You Won’t Hate by Phil Sturgeon
    ▸ RESTful Web APIs by Leonard Richardson, Mike Amundsen,
    and Sam Ruby
    ▸ Undisturbed REST by Mike Stowe
    ▸ Me!

    View Slide

  39. Thank You!
    Amanda Folson - @AmbassadorAwsum
    [email protected]

    View Slide