Skip to content

HasServiceProviderMethods Trait

The HasServiceProviderMethods trait provides standardized methods for module service providers to handle common bootstrapping tasks such as registering configurations, views, translations, and migrations.

Features

This trait offers:

  • Standardized approach to bootstrapping module components
  • Automatic registration of translations, configurations, views, and migrations
  • Integration with module interaction traits
  • Blade component namespace registration

Key Methods

bootConfigViewTranslationAndMigrations(): void

Bootstrap method that calls all of the registration methods in the correct order.

  • Behavior:
    • Registers translations
    • Registers configurations
    • Registers views
    • Loads migrations
    • Checks module interaction traits

registerTranslations(): void

Registers translation files from the module's lang directory.

  • Behavior:
    • Checks for translations in both application and module paths
    • Loads standard and JSON translations

registerConfig(): void

Registers and publishes configuration files from the module.

  • Behavior:
    • Recursively scans the module's config directory
    • Publishes all found configuration files
    • Merges module configurations with application configurations

registerViews(): void

Registers and publishes view files from the module.

  • Behavior:
    • Publishes views to the application's views directory
    • Loads views from both published and module locations
    • Registers Blade component namespace based on the module name

Usage Example

php
<?php

namespace Modules\Blog\Providers;

use Illuminate\Support\ServiceProvider;
use ModuleManager\ModuleManager\Concerns\HasServiceProviderMethods;

class BlogServiceProvider extends ServiceProvider
{
    use HasServiceProviderMethods;

    /**
     * @var string
     */
    protected $moduleName = 'Blog';

    /**
     * @var string
     */
    protected $nameLower = 'blog';

    /**
     * Boot the application events.
     */
    public function boot(): void
    {
        $this->bootConfigViewTranslationAndMigrations();

        // Additional module-specific boot logic...
    }

    /**
     * Register the service provider.
     */
    public function register(): void
    {
        // Module-specific registration logic...
    }
}

Trait Verification

The trait includes methods to verify that the application correctly uses the module's interaction traits:

  • checkIfUserUsesTrait(string $trait): void - Checks if user model uses a specific trait
  • checkIfTenantUsesTrait(string $trait): void - Checks if tenant model uses a specific trait
  • checkIfAppUsesModulesTraits(): void - Automatically scans for and verifies module interaction traits

Notes

  • This trait significantly reduces boilerplate code in module service providers
  • It follows the conventions established by the Laravel Modules package
  • The verification methods help ensure proper integration between modules and the main application
  • Most methods are designed to handle both development and production environments appropriately