Module Methods
- Name Methods
- Description and Metadata
- Path Methods
- Status Methods
- Lifecycle Methods
- Delete a Module
- JSON File Access
- Practical Examples
- Display Module Information
- Conditionally Disable a Module
- Get All Module Paths for IDE Indexing
Each module is represented by a Module instance. Retrieve one using the facade:
Copied!
use Nwidart\Modules\Facades\Module; $module = Module::find('blog');
Always check that the module exists before calling instance methods to avoid a
nullerror. UseModule::findOrFailif you want an exception on a missing module.
#Name Methods
Copied!
$module->getName(); // "Blog"$module->getLowerName(); // "blog"$module->getStudlyName(); // "Blog"$module->getKebabName(); // "blog" (or "my-blog" for multi-word names)$module->getSnakeName(); // "blog" (or "my_blog" for multi-word names) // Casting to string returns the StudlyCase name(string) $module; // "Blog"
#Description and Metadata
Copied!
$module->getDescription(); // value of "description" in module.json$module->getPriority(); // value of "priority" in module.json (int)
Read any key from module.json:
Copied!
$module->get('alias'); // "blog"$module->get('keywords'); // []$module->get('missing', 'default'); // returns 'default' if key not found
Read any key from the module's composer.json:
Copied!
$module->getComposerAttr('name'); // "nwidart/blog"$module->getComposerAttr('version');
#Path Methods
Copied!
$module->getPath(); // full path to the module directory$module->getAppPath(); // path to the module's app/ subdirectory$module->getExtraPath('routes'); // path to module/routes$module->getExtraPath('resources/views'); // path to module/resources/views
module_path() helper is equivalent:
Copied!
module_path('Blog', 'resources/views') // same as $module->getExtraPath('resources/views')
#Status Methods
Copied!
$module->isEnabled(); // true if the module is active$module->isDisabled(); // true if the module is inactive$module->isStatus(true); // true if enabled$module->isStatus(false); // true if disabled
Chain from the facade if you are confident the module exists:
Copied!
Module::find('Blog')->isEnabled();
Change status:
Copied!
$module->enable();$module->disable();$module->setActive(true); // enable$module->setActive(false); // disable
#Lifecycle Methods
Copied!
$module->register(); // register the module's service providers and aliases$module->boot(); // boot the module (fire 'boot' event)
Fire a module event manually:
Copied!
$module->fireEvent('custom-event');
#Delete a Module
Copied!
$module->delete();
#JSON File Access
Access the parsed contents of any JSON file within the module:
Copied!
$moduleJson = $module->json(); // module.json$composerJson = $module->json('composer.json');
#Practical Examples
#Display Module Information
Copied!
@foreach (Module::allEnabled() as $module) <div> <strong>{{ $module->getName() }}</strong> <span>{{ $module->getDescription() }}</span> <code>Priority: {{ $module->getPriority() }}</code> </div>@endforeach
#Conditionally Disable a Module
Copied!
$module = Module::findOrFail('Blog'); if ($someCondition) { $module->disable(); logger()->info("Blog module disabled due to condition.");}
#Get All Module Paths for IDE Indexing
Copied!
$paths = collect(Module::all())->map(fn ($m) => $m->getPath())->values()->all();
Laravel Package built by Nicolas Widart.
Maintained by David Carr follow on X @dcblogdev