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

Laravel 6, 7 and Other Goodies

Justin Yost
November 05, 2020

Laravel 6, 7 and Other Goodies

Presentation covering bits and pieces of what changed in Laravel 6 and 7, as well as covering some other pieces.

Justin Yost

November 05, 2020
Tweet

More Decks by Justin Yost

Other Decks in Technology

Transcript

  1. Laravel 7, 8 and Other Goodies Jus$n Yost Engineering Manager

    at Wirecu.er CC BY-NC 4.0 Jus-n Yost 1
  2. Laravel • One of the most popular PHP Frameworks •

    Every 6 months a new major version is released (6 months ac?ve support, 12 months bug support) CC BY-NC 4.0 Jus-n Yost 2
  3. Laravel 7 • Released in March of 2020 • Updates

    to Symfony 5 Packages • As usual a slew of new features and general improvements CC BY-NC 4.0 Jus-n Yost 3
  4. Laravel Sanctum (not Airlock) • First Party API Authen1ca1on Package

    • Provides support for straigh:orward API authen1ca1on • If you need to provide a full OAuth 2 auth server, this isn't that • Not quite as secure but probably secure enough and faster to work with • No Client Creden1al or Authoriza1on Code Grant CC BY-NC 4.0 Jus-n Yost 4
  5. Z"p HTTP Client • Z#p is a wrapper around Guzzle

    that minimizes the API and wraps it into a Laravel facade goodness Illuminate\Support\Facades\Http:: get($url, ['foo' => 'bar']); CC BY-NC 4.0 Jus-n Yost 5
  6. Z"p • Biggest feature here (imo) is reducing the workload

    to mock a response Http::fake([ // Stub a JSON response for GitHub endpoints... 'github.com/*' => Http::response(['foo' => 'bar'], 200, ['Headers']), ]); CC BY-NC 4.0 Jus-n Yost 6
  7. Fluent String Opera/ons return (string) Str::of(' This is a string')

    ->trim() ->words(2, '...') ->upper(); // THIS IS ... CC BY-NC 4.0 Jus-n Yost 8
  8. Custom Class-Based Blade Components • Class based rendering logic layer

    in Laravel • Uses the x- syntax • Public Proper:es are passed to the class • Can then modify and pass along data to a new Blade template <x-nav title="Update Your Info!"></x-nav> CC BY-NC 4.0 Jus-n Yost 9
  9. Custom Stubs for Ar-san Make php artisan stub:publish • Provides

    the common Laravel stubs that you can customize for your project next ;me you run artisan make CC BY-NC 4.0 Jus-n Yost 10
  10. Mul$ple Mail Drivers • Same as DB Drivers, mul0ple ways

    to send out an email Mail::mailer('postmark') ->send(new TransactionEmail($transaction)); Mail::mailer('mailchimp') ->send(new WelcomeEmail($transaction)); CC BY-NC 4.0 Jus-n Yost 11
  11. Rou$ng Improvements • Rou%ng, when cached, up to 2x faster

    than Laravel 6 • Model Binding to custom fields Route::get('users/{user:slug}', function (App\User $user) { return $user; }); CC BY-NC 4.0 Jus-n Yost 12
  12. Custom Eloquent Casts at Query Time • withCasts lets you

    cast fields to a custom Laravel type when you run a query $users = User::select( [ 'users.*', 'last_posted_at' => PostForUser::select(['created_at'])->get(); ], ->withCasts(['last_posted_at' => 'date']) ->get(); CC BY-NC 4.0 Jus-n Yost 13
  13. Model Changes • Adds a App\Model namespace • Model Factories

    now class based • Factories can add func7ons to do state transforma7on CC BY-NC 4.0 Jus-n Yost 15
  14. Migra&on Squashing • Squashes all the migra0ons to a single

    schema file • Then the schema runs before any new migra0ons php artisan schema:dump php artisan schema:dump --prune CC BY-NC 4.0 Jus-n Yost 16
  15. Job Batching $batch = \Illuminate\Support\Facades\Bus::batch([ new ProcessPodcast(Podcast::find(1)), new ProcessPodcast(Podcast::find(2)), ])->then(function

    (\Illuminate\Bus\Batch $batch) { // All jobs completed successfully... })->catch(function (Batch $batch, Throwable $e) { // First batch job failure detected... })->finally(function (Batch $batch) { // The batch has finished executing... })->dispatch(); return $batch->id; CC BY-NC 4.0 Jus-n Yost 17
  16. Rate Limi)ng • Rate Limi*ng is now a real rate

    limi*ng system • Rate Limi*ng can be defined based on the User, scale up and down, by IP CC BY-NC 4.0 Jus-n Yost 18
  17. Maintenance Mode • Now just a token, no IP address

    bypass lis8ng • Set an error page to render before Laravel is available in maintenance mode php artisan down --secret="AllowMe" --render="errors::503" https://example.com/AllowMe CC BY-NC 4.0 Jus-n Yost 19
  18. Backoff Queued Jobs • Enable exponen,al backoff of queued jobs

    public function backoff() { return [1, 2, 5, 10, 30]; } CC BY-NC 4.0 Jus-n Yost 21
  19. Laravel Jetstream • Applica(on scaffold for Laravel • Replaces the

    old Laravel auth scaffolding • Provides: login, registra(on, email verifica(on, 2FA, session, API and team management • Uses Tailwind for CSS • Uses Livewire or Iner(a.js for frontend rendering CC BY-NC 4.0 Jus-n Yost 22