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

Laravel Vietnam Meetup 2022: Your code is simplify or complexity?

Laravel Vietnam Meetup 2022: Your code is simplify or complexity?

Laravel Vietnam Meetup (August 2022) at Ho Chi Minh City, Vietnam

Dinh Quoc Han

March 25, 2023
Tweet

More Decks by Dinh Quoc Han

Other Decks in Technology

Transcript

  1. Your Code Is
    SIMPLIFY Or COMPLEXITY?
    with Dinh Quoc Han

    View full-size slide

  2. Code Của Bạn
    ĐƠN GIẢN Hay PHỨC TẠP?
    với Đinh Quốc Hân

    View full-size slide

  3. Who Am I?
    PHP Lover, Artisan
    6 years experience
    Currently working at SHIFT ASIA
    Admin at Laravel Viet Nam

    View full-size slide

  4. Overview
    1. Problems
    2. Coding Standards
    3. CRUD
    4. Complexity?
    5. Q&A

    View full-size slide

  5. 1. Problems!

    View full-size slide

  6. 2. Coding Standards

    View full-size slide

  7. How to follow PSR-12?

    View full-size slide

  8. composer require --dev laravel/pint

    View full-size slide

  9. 3. CRUD
    Create - Read - Update - Delete 😎

    View full-size slide

  10. M-V-C but by Packages

    View full-size slide

  11. Domain Oriented Design
    Not ‘DDD’ - Domain Driven Design 🤣

    View full-size slide

  12. Basic Example

    View full-size slide

  13. routes/web.php

    View full-size slide

  14. app/Http/Controllers/UserController.php

    View full-size slide

  15. app/Http/Controllers/UserController.php

    View full-size slide

  16. Write all business logic in controller?

    View full-size slide

  17. Controller should hold ZERO logic!

    View full-size slide

  18. Controller only bring logic together!

    View full-size slide

  19. Easy apply design patterns, structures!

    View full-size slide

  20. Easy to reuse, maintenance, testing

    View full-size slide

  21. 1. Fat Model - Thin Controller

    View full-size slide

  22. 2. Repository Class or Service Class

    View full-size slide

  23. 3. Actions/Commands Class (CQRS)

    View full-size slide

  24. Example From Laravel

    View full-size slide

  25. Next: Controller!

    View full-size slide

  26. Cruddy by Design
    Adam Wathan

    View full-size slide

  27. "More controllers doing less
    work obviates need for many
    other fancy patterns!"
    David Heinemeier Hansson - Creator of RoR

    View full-size slide

  28. 7 Standard Actions in Laravel

    View full-size slide

  29. 1. Index
    2. Create
    3. Store
    4. Show
    5. Edit
    6. Update
    7. Destroy

    View full-size slide

  30. NEVER write CUSTOM ACTION

    View full-size slide

  31. Tip #1: Nested Resource?
    Create New Controller

    View full-size slide

  32. Tip #2: Edited Dependently?
    Create New Controller

    View full-size slide

  33. Tip #3: Touches Pivot Records?
    Create New Controller

    View full-size slide

  34. Tip #4: Transitions State?
    Create New Controller

    View full-size slide

  35. Example From Laravel

    View full-size slide

  36. Or Single Action Controller?
    Invokable Controller

    View full-size slide

  37. 4. Complexity?

    View full-size slide

  38. YAGNI
    "You aren't gonna need it"

    View full-size slide

  39. Back To The BASIC

    View full-size slide

  40. app/Http/Controllers/UserController.php

    View full-size slide

  41. 1 Phút Quảng Cáo!

    View full-size slide

  42. Thank You
    Q&A

    View full-size slide