Skip to content

Lesson 3 of 9 · 24m

Modular Application Design Without Over-Architecture

Feature modules, actions, form requests, resources, and the boundaries that keep large Laravel apps navigable without inventing a framework inside the framework.

The default Laravel layout is good for a small application and misleading for a large one. app/Models, app/Http/Controllers, and app/Jobs tell you what objects are, not what the business does. A mature application organizes around business capabilities while keeping boring Laravel conventions where they help.

Group by capability, not object type

app/
  Billing/
    Actions/IssueInvoice.php
    Data/InvoiceData.php
    Models/Invoice.php
    Policies/InvoicePolicy.php
  Identity/
    Actions/InviteTeamMember.php
    Models/TeamInvitation.php
  Http/
    Controllers/Billing/InvoiceController.php
    Requests/IssueInvoiceRequest.php
    Resources/InvoiceResource.php

The exact tree matters less than the rule: everything that changes together lives together. Deleting invoicing should mean deleting a visible cluster, not hunting through eight framework-shaped drawers.

Controllers coordinate; actions do the work

final class IssueInvoice
{
    public function __construct(
        private PaymentGateway $payments,
        private InvoiceNumberGenerator $numbers,
    ) {}

    public function handle(InvoiceData $data): Invoice
    {
        return DB::transaction(function () use ($data) {
            $invoice = Invoice::create([
                'number' => $this->numbers->next(),
                'customer_id' => $data->customerId,
                'total' => $data->total,
            ]);

            $this->payments->authorize($invoice);

            return $invoice;
        });
    }
}

Controllers authenticate input, authorize the request, call one action, and shape the response. They should not contain query logic, payment logic, and mail composition in the same method.

Use Laravel's intended pressure valves

  • Form requests validate and normalize input before controllers run.
  • API resources control response shape so models do not leak internal columns.
  • Policies keep authorization close to the model but out of controllers.
  • Jobs isolate work that may be retried or delayed.
  • Events describe facts after they happen; commands and actions represent intent.

Resist premature purity

Hexagonal architecture, DTOs everywhere, repository interfaces for every model, and strict domain layers can be useful in genuinely large systems. They are harmful when applied mechanically to a five-person product team. Laravel's ORM is already your data layer; wrapping every query in a fake repository usually adds indirection without adding replaceability.

Field notes

  • A fat controller is a symptom. The disease is that application behavior has no home.
  • A model with 40 public methods is often three concepts compressed into one table-facing object.
  • Cross-module calls are dependency direction. Billing may publish an event; Identity should not reach into Billing tables directly.
  • Choose one naming convention for actions (VerbNoun) and enforce it mechanically. Inconsistent architecture is worse than imperfect architecture.

Was this lesson helpful?

Sign in to rate