hatchyu / laravel-modular-lite
Lightweight, zero-ceremony modular architecture for Laravel applications with automatic discovery, compiled caching, and familiar conventions
Requires
- php: ^8.3||^8.4||^8.5
- illuminate/console: ^11.0||^12.0||^13.0
- illuminate/database: ^11.0||^12.0||^13.0
- illuminate/routing: ^11.0||^12.0||^13.0
- illuminate/support: ^11.0||^12.0||^13.0
- illuminate/view: ^11.0||^12.0||^13.0
Requires (Dev)
- driftingly/rector-laravel: ^2.1
- laravel/pint: ^1.28
- orchestra/testbench: ^9.0||^10.0||^11.0
- pestphp/pest: ^2.34||^3.0||^4.0
- pestphp/pest-plugin-laravel: ^2.3||^3.0||^4.0
- rector/rector: ^2.3
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-10-05 14:53:45 UTC
README
A lightweight, zero-ceremony Modular Architecture package for Laravel applications. Designed for developers who want the organizational benefits of feature-based modules without the cognitive overhead of complex enterprise abstractions.
Tip
Building an Enterprise Application?
If you are building large-scale, complex enterprise platforms (ERP, CRM, Banking, Multi-domain systems) requiring strict Domain-Driven Design (DDD) 4-layer boundaries, CQRS (Actions/Queries/Data), and architectural testing, check out our flagship enterprise package:
π hatchyu/laravel-modular (composer require hatchyu/laravel-modular)
π― Design Philosophy: Stay Close to Laravel
If you are building an MVP, SaaS, eCommerce store, or internal tool, you don't always need 7 architectural layers (Domain Model, Contract, Repository, DTO, Action, Request, Resource, Controller) for a simple database record.
Laravel Modular Lite gives you:
- π Familiar Laravel Structure: Put your
Models,Controllers,Requests,Resources, andServicesin clean module folders. - β‘ Compiled Module Discovery: Eliminate runtime filesystem scanning in production with compiled manifest caching (
php artisan module:cache). - π οΈ Frictionless CRUD: Generate an entire vertical CRUD slice (
Model,Migration,Factory,Requests,Resource,Controller,Routes, andTests) in a single command. - π©Ί Self-Healing Diagnostics: Built-in
php artisan module:doctorto verify PSR-4 mappings, check route syntax, and repair missing directories automatically with--fix. - π Module Enable / Disable: Toggle modules on or off dynamically without deleting code (
php artisan module:enable/module:disable). - πΈοΈ Dependency Graph & Topological Booting: Declare dependencies in
module.json. Prerequisite modules boot before dependent ones automatically. - π‘οΈ Fail-Safe Dependency Guardrails: Runtime checks and CLI guardrails prevent broken states when dependencies are disabled or missing.
- π Safe Module Renaming: Rename modules and refactor namespaces, routes, views, providers, and other modules' dependency declarations.
- π Security Hardened: Path traversal immunity, migration table name sanitization, and strict regex module validation.
π Module Structure
When you create a module with php artisan module:make Shop, it generates a clean, conventional directory tree:
modules/
βββ Shop/
βββ Controllers/
β βββ Api/
β βββ ShopController.php
βββ Models/
β βββ Product.php
βββ Requests/
β βββ StoreProductRequest.php
β βββ UpdateProductRequest.php
βββ Resources/
β βββ ProductResource.php
βββ Services/
β βββ CheckoutService.php
βββ Database/
β βββ Migrations/
β βββ Factories/
β βββ Seeders/
βββ Routes/
β βββ web.php
β βββ api.php
βββ Views/
β βββ index.blade.php
βββ Providers/
β βββ ShopServiceProvider.php
βββ Tests/
βββ Feature/
π¦ Installation
Install the package via Composer:
composer require hatchyu/laravel-modular-lite
Add the modules PSR-4 namespace to your root composer.json:
"autoload": { "psr-4": { "App\\": "app/", "Modules\\": "modules/" } }
Then regenerate Composer's autoloader:
composer dump-autoload
(Optional) Publish the configuration file:
php artisan vendor:publish --tag="modular-lite-config"
π Module Lifecycle & Dependencies
Every module includes a lightweight module.json manifest located at modules/{ModuleName}/module.json (auto-generated by php artisan module:make):
{
"name": "Shop",
"description": "Shop module",
"version": "1.0.0",
"enabled": true,
"dependencies": [
"Inventory",
"Customers"
],
"priority": 0
}
Enabling & Disabling Modules
Easily disable modules during maintenance or feature rollout without altering codebase structure:
# Enable a module php artisan module:enable Shop # Disable a module php artisan module:disable Shop
When disabled, all of the module's routes, providers, migrations, views, config, and commands are automatically excluded from the application lifecycle.
Topological Boot Ordering
When modules declare dependencies, Laravel Modular Lite automatically sorts them topologically using Kahn's algorithm so that prerequisite modules (e.g., Inventory) are registered and booted before dependent modules (e.g., Shop).
How Disabled or Missing Dependencies Are Handled
If a module depends on another module that is disabled or missing:
- At Application Boot Time:
If
Shopis active but depends on disabledInventory, the system raises an explicit, actionable exception:ModuleDependencyException: Module [Shop] depends on module [Inventory], but [Inventory] is currently disabled. Enable it using: php artisan module:enable Inventory - At CLI Level (Dependency Protection):
- Protection Against Breaking Dependents: You cannot disable a prerequisite module if active modules depend on it:
$ php artisan module:disable Inventory Active module(s) [Shop] depend on [Inventory]. ERROR: Cannot disable module [Inventory] because active module [Shop] depends on it. Use --force to disable anyway.
- Validation on Enable: Enabling a module validates that all required dependencies are present and enabled:
$ php artisan module:enable Shop WARN: Module [Shop] depends on disabled module(s): Inventory. ERROR: Please enable prerequisite modules first or use --force to override.
- Protection Against Breaking Dependents: You cannot disable a prerequisite module if active modules depend on it:
- Health Diagnostics (
php artisan module:doctor): Displays module status and flags disabled or missing dependencies directly in the health table.
π Usage & Artisan Commands
1. Module Management
# Scaffold a new module (creates directory tree & module.json) php artisan module:make Shop # Enable or disable a module php artisan module:enable Shop php artisan module:disable Shop # List all discovered modules, status (Enabled/Disabled), and dependencies php artisan module:list # Safely rename a module and propagate new name across other modules' dependencies php artisan module:rename Shop Marketplace # Diagnose module health and auto-repair missing directories php artisan module:doctor --fix
2. Instant CRUD Generation
Generate an entire vertical slice in one command:
php artisan module:make-crud Shop Product
# or shortcut:
php artisan module:crud Shop Product
This generates:
Models/Product.phpDatabase/Migrations/xxxx_create_products_table.phpDatabase/Factories/ProductFactory.phpRequests/StoreProductRequest.php&UpdateProductRequest.phpResources/ProductResource.phpControllers/Api/ProductController.php(pure Eloquent queries)Tests/Feature/ProductControllerTest.php- Appends
Route::apiResource('products', ProductController::class);toRoutes/api.php
3. Granular Generators
# Model (supports -m, -c, -r, -f, -s, -a options) php artisan module:make-model Shop Product -a # Controller (supports --api and --resource) php artisan module:make-controller Shop ProductController --api # Requests & Resources php artisan module:make-request Shop StoreProductRequest php artisan module:make-resource Shop ProductResource # Business Service php artisan module:make-service Shop DiscountService # Migrations & Seeders php artisan module:make-migration Shop create_discounts_table php artisan module:make-seeder Shop ProductSeeder php artisan module:seed Shop # Pest / PHPUnit Test php artisan module:make-test Shop ProductTest
β‘ Production Optimization
In production environments, avoid scanning the filesystem on every request by compiling a discovery manifest:
php artisan module:cache
To clear the compiled cache:
php artisan module:clear
(Integrated with native php artisan optimize and php artisan optimize:clear).
βοΈ When to Choose Lite vs Enterprise
| Need | laravel-modular-lite (This Package) |
laravel-modular (Enterprise) |
|---|---|---|
| Ideal For | Startups, SaaS, eCommerce, CRUD apps | ERP, CRM, Banking, Complex Domains |
| Layering | Simple folders (Models, Controllers, Services) |
Strict 4-Layer DDD (Domain, Application, Interface, Infrastructure) |
| Data Flow | Direct Eloquent queries in controllers/services | CQRS (Actions, Queries, DTOs, Repository Interfaces) |
| Guardrails | Conventional Laravel | Architectural boundary enforcement via Pest Arch |
π§ͺ Testing
composer test
π¨ Code Style
composer format:check composer format
π License
The MIT License (MIT). Please see License File for more information.