Facade Methods
- Retrieving Modules
- Get All Modules
- Get All Enabled Modules
- Get All Disabled Modules
- Get Ordered Modules
- Get Modules as a Collection
- Filter by Status
- Scan for Modules
- Finding a Module
- Find by Name
- Find or Fail
- Check Existence
- Checking Module Status
- Enabling and Disabling
- Counting Modules
- Path Methods
- CLI Session Helpers
- Installing and Updating
- Delete a Module
- Configuration
- Macros
- Practical Examples
- Render a Navigation Menu from Enabled Modules
- Check a Module Before Using Its Features
- Get Scan Paths
The Module facade provides static access to the module repository. Import it at the top of your file:
use Nwidart\Modules\Facades\Module;
#Retrieving Modules
#Get All Modules
Returns an array of all modules (enabled and disabled):
$modules = Module::all();
#Get All Enabled Modules
$activeModules = Module::allEnabled();
#Get All Disabled Modules
$inactiveModules = Module::allDisabled();
#Get Ordered Modules
Returns modules sorted by the priority field in module.json. Lower numbers come first:
$modules = Module::getOrdered(); // ascending (default)$modules = Module::getOrdered('desc'); // descending
#Get Modules as a Collection
$collection = Module::toCollection();$collection = Module::collections(); // enabled modules only
#Filter by Status
Get modules by status (1 = enabled, 0 = disabled):
$enabled = Module::getByStatus(1);$disabled = Module::getByStatus(0);
#Scan for Modules
Scan the filesystem for modules (re-reads the disk, bypassing any cache):
$modules = Module::scan();
#Finding a Module
#Find by Name
Returns the Module instance or null if not found:
$module = Module::find('blog');
#Find or Fail
Returns the Module instance or throws Nwidart\Modules\Exceptions\ModuleNotFoundException:
$module = Module::findOrFail('blog');
#Check Existence
if (Module::has('blog')) { // module exists}
#Checking Module Status
Module::isEnabled('Blog'); // boolModule::isDisabled('Blog'); // bool
#Enabling and Disabling
Module::enable('Blog');Module::disable('Blog');
#Counting Modules
$total = Module::count();
#Path Methods
Module::getPath(); // path to the Modules root directoryModule::getModulePath('Blog'); // path to the Blog module directoryModule::assetPath('Blog'); // path to the Blog module's public assetsModule::getAssetsPath(); // path to the modules asset directoryModule::asset('blog:img/logo.png'); // URL to a module asset
#CLI Session Helpers
These methods track which module is "in use" for the current CLI session, allowing you to omit the module name on generator commands after running module:use:
Module::setUsed('Blog');Module::getUsedNow(); // returns 'Blog'Module::getUsedStoragePath(); // path to the .modulesuse session file
#Installing and Updating
Install a module from a Composer package name:
Module::install('nwidart/article-module');
Update a module's Composer dependencies:
Module::update('Blog');
#Delete a Module
Permanently deletes the module directory:
Module::delete('Blog');
#Configuration
Read values from the laravel-modules package configuration:
Module::config('composer.vendor'); // reads config/modules.php
#Macros
Extend the module repository with custom methods:
Module::macro('enabledCount', function () { return count($this->allEnabled());}); // Call it$count = Module::enabledCount();
#Practical Examples
#Render a Navigation Menu from Enabled Modules
@foreach (Module::allEnabled() as $module) <a href="{{ route($module->getLowerName() . '.index') }}"> {{ $module->getName() }} </a>@endforeach
#Check a Module Before Using Its Features
if (Module::isEnabled('Shop')) { $cartCount = app(\Modules\Shop\Services\CartService::class)->count();}
#Get Scan Paths
$paths = Module::getScanPaths(); // directories scanned for modules
Laravel Package built by Nicolas Widart.
Maintained by David Carr follow on X @dcblogdev