Service Provider
Similar to Laravel, a module can include one or various service providers. Usually, service providers are used to registering and booting services. It's also the place where you'll use your filters, actions, and events to mutate, catch, and listen to them.
Service Providers are automatically loaded when a module is enabled, and all the services and other configurations it defines are injected into NexoPOS's request lifecycle.
Throughout this, we'll assume our module namespace is "MyModule".
Creating Service Providers
A service provider must be created within the folder "Providers" of the module. The name of the class and the file should match to ensure compatibility with the PSR-4 Autoloading.
<?php
namespace Modules\MyModule\Providers;
use Illuminate\Support\ServiceProvider as CoreServiceProvider;
class ServiceProvider extends CoreServiceProvider
{
// ...
}
Method: register
The register method is the place where the service container is used to register things (services). It's also the place where you can listen to events and register your actions and filters.
<?php
namespace Modules\MyModule\Providers;
use Illuminate\Support\ServiceProvider as CoreServiceProvider;
use Illuminate\Support\Facades\Event;
use App\Events\AfterSuccessfulLoginEvent;
use Modules\MyModule\Filters\MyFilters;
use Modules\MyModule\Events\MyEvent;
class ServiceProvider extends CoreServiceProvider
{
public function register()
{
// registering a module
Hook::filter( 'ns.dashboard-menus', [ MyFilters::class, 'addMenus' ]);
// registering a service
$this->app->singleton( MyModuleService::class, fn() => new MyModuleClass( ns()->option->get( 'custom-option' ) ) );
// catch events
Event::listen( AfterSuccessfulLoginEvent::class, [ MyEvent::class, 'catchSuccessfulLogin' ]);
}
}
Method: boot
The same logic that works on the Laravel boot method from service providers applies to this method as well. This method is called right after the method "register" of all enabled modules.