alizharb / laravel-modular
A professional, framework-agnostic modular architecture for Laravel 11+. Features zero-config autoloading, 29+ Artisan command overrides, and seamless Vite integration.
Fund package maintenance!
alizharb
Installs: 289
Dependents: 4
Suggesters: 4
Security: 0
Stars: 11
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/alizharb/laravel-modular
Requires
- php: ^8.2
- illuminate/contracts: ^11.0|^12.0
- spatie/laravel-package-tools: ^1.16
- wikimedia/composer-merge-plugin: ^2.1
Requires (Dev)
- driftingly/rector-laravel: ^2.1
- larastan/larastan: ^3.9
- laravel/pint: ^1.24
- mockery/mockery: *
- orchestra/testbench: ^9.5|^10.0
- pestphp/pest: *
- pestphp/pest-plugin-laravel: *
- phpstan/phpstan: *
- phpunit/phpunit: *
- rector/rector: *
- spatie/phpunit-snapshot-assertions: ^5.1
Suggests
- alizharb/laravel-hooks: For modular hook system support
- alizharb/laravel-modular-filament: For Filament admin panel integration in modules
- alizharb/laravel-modular-livewire: For Livewire component support in modules
- alizharb/laravel-themer: For advanced theme management support
README
Laravel Modular is a professional, framework-agnostic modular system engineered for Laravel 11/12. It empowers you to build scalable, strictly typed, and decoupled applications with zero configuration overhead.
We override 29+ native Artisan commands to provide a seamless "first-class" modular experience, feeling exactly like standard Laravel but better.
โจ Features
- ๐๏ธ Native Experience: 29+ Artisan commands (
make:model,make:controller, etc.) fully support--module. - โก Zero Config Autoloading: Intelligent
composer-merge-pluginintegration for isolated module dependencies. - ๐ Performance First: Built-in discovery caching (
modular:cache) for near-zero overhead in production. - ๐ Dynamic Activation: Enable or disable modules on the fly via
module:enableandmodule:disable. - ๐ Auto-Discovery: Automatic registration of Artisan commands, Policies, and Event Listeners within modules.
- ๐ Decoupled Architecture: Strictly typed
ModuleRegistryand traits for maximum stability. - ๐ ๏ธ Full Customizability: Publishable stubs, dynamic config paths, and global helpers.
- โ Laravel 11 & 12 Ready: Optimized for PHP 8.2+ and the latest framework features.
- ๐จ Asset Management: Seamless Vite integration via
modular_vite()and asset linking.
๐ Ecosystem
Enhance your modular application with our official packages:
- Laravel Hooks: specific modular hook system support.
- Filament Integration: Seamless Filament admin panel integration in modules.
- Livewire Integration: First-class Livewire component support in modules.
- Laravel Themer: Advanced theme management system.
๐ Installation
Install the package via Composer:
composer require alizharb/laravel-modular
Run the installation command to automatically configure your application:
php artisan modular:install
Note: This will automatically install and configure
wikimedia/composer-merge-pluginto handle your module dependencies.
Manual Setup
If you prefer to configure things manually, follow these steps:
1. Composer Autoloading
Add the following to your root composer.json to ensure module namespaces are autoloaded:
"autoload": { "psr-4": { "App\\": "app/", "Modules\\": "modules/" } }, "extra": { "merge-plugin": { "include": [ "modules/*/composer.json" ] } }
2. Vite Configuration
To enable hot-readling for module assets, create a vite.modular.js file in your root and update vite.config.js:
// vite.config.js import { defineConfig } from 'vite'; import laravel from 'laravel-vite-plugin'; import { modularLoader } from './vite.modular.js'; export default defineConfig({ plugins: [ laravel({ input: [ 'resources/css/app.css', 'resources/js/app.js', ...modularLoader.inputs() // Add this line ], refresh: [ ...modularLoader.refreshPaths() // Add this line ], }), ], });
๐ Usage
Creating a Module
Generate a fully structured module in seconds:
php artisan make:module Blog
Generating Resources
Every standard Laravel make: command acts as a modular command when you pass the --module flag:
# Create a Model with Migration, Controller, and Factory in 'Blog' module php artisan make:model Post --module=Blog -mcf # Create a resource controller php artisan make:controller API/PostController --module=Blog --api
Modular Database
Run migrations and seeders specifically for your modules:
# Migrate all modules php artisan modular:migrate # Migrate a specific module php artisan modular:migrate Blog --fresh --seed # Run module seeders php artisan modular:seed Blog # Check for circular dependencies php artisan modular:check # Debug module configuration php artisan modular:debug Blog # Run module tests php artisan modular:test Blog
Middleware & Config
Define middleware in your module.json:
"middleware": { "web": ["Modules\\Blog\\Http\\Middleware\\TrackVisits"], "blog.admin": "Modules\\Blog\\Http\\Middleware\\AdminGuard" }
Access config case-insensitively:
// Both work! config('Blog::settings.key'); config('blog::settings.key');
๐ ๏ธ Helpers & Assets
Global Helpers
Access module information globally with strictly typed helpers:
// Get the registry or specific module config $modules = module(); $blogConfig = module('Blog'); // Get absolute path to a resource $viewPath = module_path('Blog', 'Resources/views'); // Get absolute path to a config file $configPath = module_config_path('Blog', 'settings.php');
Asset Management
Link your module assets to public/modules for easy serving:
php artisan modular:link
Use the helper to generate asset URLs in your Blade views:
<link rel="stylesheet" href="{{ module_asset('Blog', 'css/app.css') }}"> <img src="{{ module_asset('Blog', 'images/logo.png') }}" alt="Blog Logo">
โ๏ธ Configuration
Publish the configuration file for advanced customization:
php artisan vendor:publish --tag="modular-config"
You can customize:
- Paths: Move modules to
packages/or any custom directory. - Stubs: Enable custom stubs to strictly enforce your team's coding standards.
- Composer: Set default fields (
vendor,author,license) for generatedcomposer.jsonfiles.
๐งช Testing
We strictly enforce testing. Use the provided test suite to verify your modules:
vendor/bin/pest
๐ Sponsors
We would like to extend our thanks to the following sponsors for funding Laravel Modular development. If you are interested in becoming a sponsor, please visit the Laravel Modular GitHub Sponsors page.
๐ค Contributing
We welcome contributions! Please see CONTRIBUTING for details.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
๐ Acknowledgments
- Laravel: For creating the most elegant PHP framework.
- Spatie: For setting the standard on Laravel package development.
๐ Security
If you discover any security-related issues, please email Ali Harb at harbzali@gmail.com.
๐ License
The MIT License (MIT). Please see License File for more information.
Made with โค๏ธ by Ali Harb
