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.
Add a method to the repository
Section titled “Add a method to the repository”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.
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']Add a method to every module
Section titled “Add a method to every module”Register the macro on Nwidart\Modules\Module. Inside the closure, $this is the module.
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'Check the result
Section titled “Check the result”Test the macros in Tinker:
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.