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. Laravel
    PHP Conference Japan 2023
    2023.10.08
    Yuta Ohashi

    View full-size slide

  2. 2
    • Laravel
    • Laravel
    • Laravel

    View full-size slide

  3. 4
    Laravel
    PHP Web
    • 2010 6 1
    • 10 2023 2
    • 2
    • 2024 1 11


    View full-size slide

  4. 5
    Laravel
    [ ] Google Trends
    Laravel
    Symfony
    CakePHP

    View full-size slide

  5. 6
    Laravel
    Laravel - 3 -






    View full-size slide

  6. 8
    Laravel
    • Eloquent
    • Facede

    • Artisan

    View full-size slide

  7. 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

    View full-size slide

  8. 10
    Laravel
    Route
    Route::post('/tsubuyaki', function ( ) {
    });
    routes/web.php

    View full-size slide

  9. 11
    Laravel
    Route
    Route::post('/tsubuyaki', function ( ) {
    });
    routes/web.php
    /tsubuyaki

    View full-size slide

  10. 12
    Laravel
    Route
    Route::post('/tsubuyaki', function ( ) {
    });
    routes/web.php
    /tsubuyaki
    POST

    View full-size slide

  11. 13
    Laravel
    Route
    Route::post('/tsubuyaki', function ( ) {
    });
    routes/web.php
    /tsubuyaki
    POST
    function

    View full-size slide

  12. 14
    Laravel
    Dependency Injection Request
    Request $request
    $text = $request->text;
    routes/web.php

    View full-size slide

  13. 15
    Laravel
    Dependency Injection Request
    Request $request
    $text = $request->text;
    routes/web.php
    Request
    DI

    View full-size slide

  14. 16
    Laravel
    Dependency Injection Request
    Request $request
    $text = $request->text;
    routes/web.php
    Request
    DI

    $request->text

    View full-size slide

  15. 17
    Laravel
    Eloquent
    $tsubuyaki = new Tsubuyaki;
    $tsubuyaki->text = $text;
    $tsubuyaki->save();
    routes/web.php

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  19. 21
    Laravel
    redirect
    return redirect('tsubuyaki/complete');
    routes/web.php

    View full-size slide

  20. 22
    Laravel
    redirect
    return redirect('tsubuyaki/complete');
    routes/web.php

    View full-size slide

  21. 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

    View full-size slide

  22. 24
    Laravel
    Mailer $mailer
    $mailer->send(new TsubuyakiCreated());
    routes/web.php

    View full-size slide

  23. 25
    Laravel
    Mailer $mailer
    $mailer->send(new TsubuyakiCreated());
    routes/web.php
    Mailer
    DI

    View full-size slide

  24. 26
    Laravel
    Mailer $mailer
    $mailer->send(new TsubuyakiCreated());
    routes/web.php
    Mailer
    DI
    Mailable TsubuyakiCreated
    artisan

    View full-size slide

  25. 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

    View full-size slide

  26. 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

    $request->text
    $text
    $tsubuyaki->text text
    save()
    Mailer
    DI
    Mailable TsubuyakiCreated
    artisan
    Request
    DI
    Eloquent
    artisan

    View full-size slide

  27. 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

    $request->text
    Eloquent
    $text
    $tsubuyaki->text text
    save()
    Mailer
    DI
    Request
    DI
    Mailable TsubuyakiCreated
    artisan

    View full-size slide

  28. 30
    Laravel


    • Laravel Way 1

    View full-size slide

  29. 31
    Laravel



    • 範
    • Eloquent ActiveRecode




    View full-size slide

  30. 33
    Laravel
    Laravel

    • Laravel
    • Laravel
    • Laravel


    • Interface

    View full-size slide

  31. 34
    Laravel
    Laravel
    Laravel

    View full-size slide

  32. 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

    View full-size slide

  33. 36
    Laravel
    Mailer $mailer
    $mailer->send(new TsubuyakiCreated());
    app/Http/Controllers/TsubuyakiController.php
    Illuminate¥Mail¥Mailer

    View full-size slide

  34. 37
    Laravel
    app/Http/Controllers/TsubuyakiController.php
    lib TsubuyakiCreatedSenderInterface
    namespace Lib¥Mail;
    interface TsubuyakiCreatedSenderInterface {
    public function send(): void;
    }
    lib/Mail/TsubuyakiCreatedSenderInterface.php

    View full-size slide

  35. 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

    View full-size slide

  36. 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

    View full-size slide

  37. 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

    View full-size slide

  38. 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

    View full-size slide

  39. 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

    View full-size slide

  40. 43
    Laravel
    $tsubuyaki = new Tsubuyaki();
    $tsubuyaki->text = $text;
    $tsubuyaki->save();
    app/Http/Controllers/TsubuyakiController.php
    Eloquent

    View full-size slide

  41. 44
    Laravel
    app/Http/Controllers/TsubuyakiController.php
    namespace Lib¥Tsubuyaki;
    interface TsubuyakiUseCaseInterface {
    public function save(string $text): void;
    }
    lib/Tsubuyaki/TsubuyakiUseCaseInterface.php
    lib TsubuyakiUseCaseInterface

    View full-size slide

  42. 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

    View full-size slide

  43. 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

    View full-size slide

  44. 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

    View full-size slide

  45. 48
    Laravel
    Interface

    • app Laravel
    • lib Laravel
    • Laravel app
    • lib

    • DB

    View full-size slide

  46. 49
    Laravel
    1
    • lib Doctrine
    • Doctrine ORM
    Eloquent
    • Doctrine

    • Eloquent DB

    View full-size slide

  47. 50
    Laravel
    2 1


    • UseCase Service

    • DI

    View full-size slide

  48. 51
    Laravel
    3 2

    • Mailgun SendGrid
    • Interface
    • MailerResolver

    • MailerResolver

    • 1

    View full-size slide

  49. 52
    Laravel
    • Laravel Way

    • PoC





    View full-size slide

  50. 53
    Laravel

    • SOLID
    etc




    View full-size slide

  51. 55
    • Laravel
    • PHP
    • [ ]
    • Laravel
    • Facade Eloquent

    • Laravel
    • Interface


    View full-size slide

  52. / @blue_goheimochi
    , , ,
    , PUBG MOBILE,

    View full-size slide