Skip to content

Extend the repository and module classes with macros

To add a method of your own to the module repository or to a module, register a macro. Both classes use Laravel’s Macroable trait. The package registers no macros.

The Module facade forwards to the repository. Register the macro in the boot() method of a service provider of your application. Inside the closure, $this is the repository.

app/Providers/AppServiceProvider.php
use Nwidart\Modules\Facades\Module;
public function boot(): void
{
Module::macro('enabledNames', fn () => array_keys($this->allEnabled()));
}

Call it through the facade:

Module::enabledNames(); // ['blog', 'shop']

Register the macro on Nwidart\Modules\Module. Inside the closure, $this is the module.

app/Providers/AppServiceProvider.php
use Nwidart\Modules\Module as ModuleClass;
public function boot(): void
{
ModuleClass::macro('routePrefix', fn () => 'api/'.$this->getLowerName());
}

Call it on a module:

Module::findOrFail('Blog')->routePrefix(); // 'api/blog'

Test the macros in Tinker:

Terminal window
php artisan tinker --execute="echo json_encode(Module::enabledNames()), PHP_EOL; echo Module::findOrFail('Blog')->routePrefix(), PHP_EOL;"

The output lists the names of the enabled modules, then api/blog. Module::hasMacro('enabledNames') returns true.