alizharb/laravel-modular-livewire
Installation:
composer require alizharb/laravel-modular-livewire
Publish the config (if needed):
php artisan vendor:publish --provider="AlizHarb\ModularLivewire\ModularLivewireServiceProvider"
First Use Case: Create a Livewire component inside a module:
php artisan make:livewire Blog/PostList --module
This generates:
Modules/Blog/app/Livewire/PostList.php
Modules/Blog/resources/views/livewire/post-list.blade.php
Usage in Blade:
<livewire:blog::post-list />
The package auto-discovers and registers the component under the module namespace (blog::).
Modular Structure:
Place Livewire components in Modules/{ModuleName}/app/Livewire/.
Example:
Modules/Blog/app/Livewire/
├── PostList.php
├── PostCreate.php
└── ...
Namespace Prefixing:
The package auto-prefixes components with the module name (e.g., blog::post-list).
Override in config/modular-livewire.php if needed:
'namespace_prefix' => 'modules.',
Generate Components:
# Inside a module directory
php artisan make:livewire Admin/UserManager --module
# Or globally (auto-detects module)
php artisan make:livewire --module Blog/PostList
Run Commands:
Use the --module flag to target specific modules:
php artisan livewire:discover --module=Blog
Module Bootstrapping:
The package hooks into Laravel Modular’s ModuleServiceProvider to auto-register components when modules boot.
No manual registration in AppServiceProvider required.
Dynamic Module Loading: Components are discovered only when their module is loaded (lazy-loading friendly).
Views:
Store views in Modules/{ModuleName}/resources/views/livewire/{ComponentName}.blade.php.
Example:
<!-- Modules/Blog/resources/views/livewire/post-list.blade.php -->
<div>Posts: {{ $posts->count() }}</div>
Assets: Use the module’s asset paths in views:
<script src="{{ asset('modules/blog/js/post-list.js') }}"></script>
Unit Testing: Mock components using their fully qualified class names:
$this->livewire(Blog\Livewire\PostList::class);
Feature Testing: Use the module namespace in assertions:
$this->livewire('blog::post-list')->assertSee('Posts');
Namespace Collisions:
Admin/UserList and Blog/UserList), the last discovered module will override the first.namespace_prefix in config.Caching Issues:
php artisan livewire:discover --force
to clear the cache and re-register components.Module Not Discovered:
ModuleServiceProvider is registered in config/app.php under providers.composer.json includes the Livewire directory in autoload.psr-4:
"autoload": {
"psr-4": {
"Modules\\Blog\\": "Modules/Blog/src/"
}
}
Livewire Publish Not Working:
make:livewire doesn’t generate files in the module, ensure you’re running the command from the project root or specify the module:
php artisan make:livewire --module=Blog/PostList
Check Discovery:
Run php artisan livewire:discover --verbose to see which components are being registered.
Log Component Registration:
Enable debug mode in config/modular-livewire.php:
'debug' => env('MODULAR_LIVEWIRE_DEBUG', false),
Custom Component Paths:
Override the default Livewire path in config/modular-livewire.php:
'component_path' => 'Modules/{ModuleName}/app/Livewire',
Shared Components:
For components shared across modules, place them in a shared module (e.g., Core) and use:
<livewire:core::shared-component />
Dynamic Module Loading:
Leverage Laravel Modular’s Module::load() to dynamically load components:
Module::load('Blog');
// Now <livewire:blog::post-list /> is available
Performance:
The package caches discovered components. For development, disable caching in config/modular-livewire.php:
'cache_discovered_components' => env('APP_ENV') !== 'local',
Extending the Package:
AlizHarb\ModularLivewire\Discovery\LivewireDiscoverer to add custom logic.make:livewire command by publishing and modifying the template:
php artisan vendor:publish --tag=modular-livewire-templates
How can I help you explore Laravel packages today?