Skip to content

Instantly share code, notes, and snippets.

@jpswade
Created November 18, 2025 15:57
Show Gist options
  • Select an option

  • Save jpswade/e7c5155f5e5a928d0d11f4256517dac9 to your computer and use it in GitHub Desktop.

Select an option

Save jpswade/e7c5155f5e5a928d0d11f4256517dac9 to your computer and use it in GitHub Desktop.
Laravel Modular Architecture

Laravel Modular Architecture

This document outlines the architectural decisions and patterns used in our Laravel-based applications. It serves as a guide for understanding how we structure our code and make architectural decisions.

Core Principles

  1. Modular Design: Organize code around business domains
  2. SOLID Principles: Follow software design principles for maintainable code
  3. Domain-Driven Design: Structure code around business domains
  4. Progressive Enhancement: Start simple, evolve as needed

Application Structure

Modular Architecture

We use a modular approach to organize our Laravel applications. Each module represents a distinct business domain and follows a consistent structure:

src/
  ModuleName/
    Console/
      Commands/
    Controllers/
    Models/
    Services/
    Routes/
      web.php
      api.php
    README.md

Module Components

  • Models: Domain entities and relationships
  • Controllers: HTTP request/response handling
  • Services: Business logic and domain operations
  • Commands: Console tasks and scheduled jobs
  • Routes: Module-specific endpoints
  • README.md: Module documentation

Example: Currency Exchange Module

src/
  CurrencyExchange/
    Console/
      Commands/
        DownloadCurrencyRates.php
    Models/
      CurrencyRate.php
    Services/
      CurrencyService.php
    Routes/
      api.php
    README.md

Design Patterns

Model-View-Controller (MVC)

We follow Laravel's MVC pattern:

  • Models: Business logic and data access
  • Views: Presentation layer (Blade templates)
  • Controllers: Request handling and response generation

Service Layer

We use services to:

  • Encapsulate complex business logic
  • Provide reusable functionality
  • Keep controllers thin
  • Enable better testing

Repository Pattern

Repositories are used to:

  • Abstract data access
  • Provide a consistent interface
  • Make testing easier
  • Centralize query logic

Best Practices

Code Organization

  1. Module Independence: Keep modules as independent as possible
  2. Clear Interfaces: Define clear boundaries between modules
  3. Shared Code: Use a common module for shared functionality
  4. Documentation: Maintain README files for each module
  5. Testing: Write tests at the module level

When to Create Modules

Create a new module when:

  • The functionality represents a distinct business domain
  • The code could be reused in other projects
  • The feature set is large enough to warrant separation
  • Different teams will work on different parts

Module Communication

Modules communicate through:

  • Service classes
  • Events and listeners
  • Shared interfaces
  • Message queues
  • API endpoints

Development Approach

Starting Simple

We begin with a monolithic application and:

  • Organize code into modules
  • Keep related code together
  • Maintain clear boundaries
  • Make modules self-contained

Progressive Enhancement

As the application grows:

  • Extract modules when needed
  • Maintain clear interfaces
  • Keep documentation up to date
  • Consider microservices for specific domains

Testing Strategy

  • Unit tests for business logic
  • Feature tests for HTTP endpoints
  • Integration tests for module interactions
  • End-to-end tests for critical paths

Documentation

  • Each module has its own README
  • Architecture decisions are documented
  • API documentation is maintained
  • Code is self-documenting where possible

Further Reading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment