Skip to content

HasModuleName Trait

The HasModuleName trait provides functionality for automatically detecting and retrieving the module name from a class. This is particularly useful for components that need to know which module they belong to.

Features

This trait offers:

  • Automatic module name detection from class namespace
  • Caching of the module name to improve performance
  • Ability to override the detected module name

Methods

getModuleName(): string

Returns the module name associated with the class.

  • Returns:
    • The module name in lowercase format
  • Behavior:
    • First checks if a static $moduleName property is set on the class
    • If not set, automatically detects the module name from the class namespace
    • Converts the module name to lowercase
    • Caches the result for future calls

Usage Example

php
<?php

namespace Modules\Blog\Filament\Resources;

use ModuleManager\ModuleManager\Concerns\HasModuleName;

class PostResource
{
    use HasModuleName;

    public function someMethod()
    {
        // This will return "blog"
        $moduleName = self::getModuleName();

        // Use the module name for configuration, path generation, etc.
        $configKey = $moduleName . '.some_config';

        return config($configKey);
    }
}

Manual Override

You can manually set the module name by defining a static property in your class:

php
<?php

namespace App\Resources;

use ModuleManager\ModuleManager\Concerns\HasModuleName;

class CustomResource
{
    use HasModuleName;

    // Override the auto-detected module name
    protected static ?string $moduleName = 'blog';
}

Integration

This trait is often used in combination with other traits like:

  • HasConfigurableItems - for configuration lookups
  • HasTablePrefix - for table name prefixing
  • HasServiceProviderMethods - for service provider functionality

Notes

  • The module name is extracted from the namespace, typically from the format Modules\ModuleName\...
  • The trait is designed to work seamlessly with the Module Manager's module structure
  • Module names are always converted to lowercase for consistency