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

Laravelのコードとわたしたちのコードとの距離を保つ / phpcon2023

Laravelのコードとわたしたちのコードとの距離を保つ / phpcon2023

PHP Conference Japan 2023の登壇資料です。
https://phpcon.php.gr.jp/2023/

blue_goheimochi

October 08, 2023
Tweet

More Decks by blue_goheimochi

Other Decks in Technology

Transcript

  1. 4 Laravel PHP Web • 2010 6 1 • 10

    2023 2 • 2 • 2024 1 11 • •
  2. 9 Laravel Route::post('/tsubuyaki', function (Request $request) { $text = $request->text;

    $tsubuyaki = new Tsubuyaki; $tsubuyaki->text = $text; $tsubuyaki->save(); return redirect('tsubuyaki/complete'); }); routes/web.php
  3. 16 Laravel Dependency Injection Request Request $request $text = $request->text;

    routes/web.php Request DI <input value= text name= text value= /> $request->text
  4. 18 Laravel Eloquent $tsubuyaki = new Tsubuyaki; $tsubuyaki->text = $text;

    $tsubuyaki->save(); routes/web.php Eloquent artisan
  5. 19 Laravel Eloquent $tsubuyaki = new Tsubuyaki; $tsubuyaki->text = $text;

    $tsubuyaki->save(); routes/web.php $text $tsubuyaki->text text Eloquent artisan
  6. 20 Laravel Eloquent $tsubuyaki = new Tsubuyaki; $tsubuyaki->text = $text;

    $tsubuyaki->save(); routes/web.php $text $tsubuyaki->text text save() Eloquent artisan
  7. 23 Laravel Route::post('/tsubuyaki', function (Request $request) { $text = $request->text;

    $tsubuyaki = new Tsubuyaki; $tsubuyaki->text = $text; $tsubuyaki->save(); return redirect('tsubuyaki/complete'); }); routes/web.php
  8. 27 Laravel Route::post('/tsubuyaki', function (Request $request, Mailer $mailer) { $text

    = $request->text; $tsubuyaki = new Tsubuyaki; $tsubuyaki->text = $text; $tsubuyaki->save(); $mailer->send(new TsubuyakiCreated()); return redirect('tsubuyaki/complete'); }); routes/web.php
  9. 28 Laravel Route::post('/tsubuyaki', function (Request $request, Mailer $mailer) { $text

    = $request->text; $tsubuyaki = new Tsubuyaki; $tsubuyaki->text = $text; $tsubuyaki->save(); $mailer->send(new TsubuyakiCreated()); return redirect('tsubuyaki/complete'); }); routes/web.php /tsubuyaki POST function <input name= text value= /> $request->text $text $tsubuyaki->text text save() Mailer DI Mailable TsubuyakiCreated artisan Request DI Eloquent artisan
  10. 29 Laravel Route::post('/tsubuyaki', function (Request $request, Mailer $mailer) { $text

    = $request->text; $tsubuyaki = new Tsubuyaki; $tsubuyaki->text = $text; $tsubuyaki->save(); $mailer->send(new TsubuyakiCreated()); return redirect('tsubuyaki/complete'); }); routes/web.php /tsubuyaki POST function <input name= text value= /> $request->text Eloquent $text $tsubuyaki->text text save() Mailer DI Request DI Mailable TsubuyakiCreated artisan
  11. 35 Laravel class TsubuyakiController extends Controller { public function store(Request

    $request, Mailer $mailer): RedirectResponse { $text = $request->text; $tsubuyaki = new Tsubuyaki(); $tsubuyaki->text = $text; $tsubuyaki->save(); $mailer->send(new TsubuyakiCreated()); return redirect('tsubuyaki/complete'); } } app/Http/Controllers/TsubuyakiController.php routes/web.php
  12. 38 Laravel use Lib¥Mail¥TsubuyakiCreatedSenderInterface as SenderInterface; SenderInterface $sender $sender->send(); app/Http/Controllers/TsubuyakiController.php

    namespace Lib¥Mail; interface TsubuyakiCreatedSenderInterface { public function send(): void; } lib/Mail/TsubuyakiCreatedSenderInterface.php Interface
  13. 39 Laravel use Lib¥Mail¥TsubuyakiCreatedSenderInterface as SenderInterface; SenderInterface $sender $sender->send(); app/Http/Controllers/TsubuyakiController.php

    Interface Mailer namespace Lib¥Mail; interface TsubuyakiCreatedSenderInterface { public function send(): void; } lib/Mail/TsubuyakiCreatedSenderInterface.php
  14. 40 Laravel class TsubuyakiCreatedSender implements TsubuyakiCreatedSenderInterface { public function __construct(private

    readonly Mailer $mailer) {} public function send(): void { $this->$mailer->send(new TsubuyakiCreated()); } } app/Mail/TsubuyakiCreatedSender.php TsubuyakiCreatedSenderInterface app
  15. 41 Laravel class TsubuyakiCreatedSender implements TsubuyakiCreatedSenderInterface { public function __construct(private

    readonly Mailer $mailer) {} public function send(): void { $this->$mailer->send(new TsubuyakiCreated()); } } app/Mail/TsubuyakiCreatedSender.php class AppServiceProvider extends ServiceProvider { public array $bindings = [ Lib¥Mail¥TsubuyakiCreatedSenderInterface::class => ¥App¥Mail¥TsubuyakiCreatedSender::class, ]; } app/Providers/AppServiceProvider.php
  16. 42 Laravel use Lib¥Mail¥TsubuyakiCreatedSenderInterface as SenderInterface; class TsubuyakiController extends Controller

    { public function store(Request $request, SenderInterface $sender): RedirectResponse { $text = $request->text; $tsubuyaki = new Tsubuyaki(); $tsubuyaki->text = $text; $tsubuyaki->save(); $sender->send(); return redirect('tsubuyaki/complete'); } } app/Http/Controllers/TsubuyakiController.php
  17. 44 Laravel app/Http/Controllers/TsubuyakiController.php namespace Lib¥Tsubuyaki; interface TsubuyakiUseCaseInterface { public function

    save(string $text): void; } lib/Tsubuyaki/TsubuyakiUseCaseInterface.php lib TsubuyakiUseCaseInterface
  18. 45 Laravel ; use Lib¥Tsubuyaki¥TsubuyakiUseCaseInterface; TsubuyakiUseCaseInterface $useCase $useCase->save($text); app/Http/Controllers/TsubuyakiController.php namespace

    Lib¥Tsubuyaki; interface TsubuyakiUseCaseInterface { public function save(string $text): void; } lib/Tsubuyaki/TsubuyakiUseCaseInterface.php Interface Eloquent
  19. 46 Laravel class TsubuyakiUseCase implements TsubuyakiUseCaseInterface { public function save(string

    $text): void { $tsubuyaki = new Tsubuyaki(); $tsubuyaki->text = $text; $tsubuyaki->save(); } } app/Tsubuyaki/TsubuyakiUseCase.php TsubuyakiUseCaseInterface app
  20. 47 Laravel class TsubuyakiUseCase implements TsubuyakiUseCaseInterface { public function __construct(private

    readonly EntityManager $em) {} public function save(string $text): void { $tsubuyaki = new Tsubuyaki(); $tsubuyaki->setText($text); $em->persist($tsubuyaki); $em->flush(); } } app/Tsubuyaki/TsubuyakiUseCase.php Doctrine
  21. 51 Laravel 3 2 • • Mailgun SendGrid • Interface

    • MailerResolver • • MailerResolver • • 1
  22. 55 • Laravel • PHP • [ ] • Laravel

    • Facade Eloquent • • Laravel • Interface • •