Filament Modules for Laravel

repository·5.x·Indexed 42 days ago

https://github.com/coolsam726/filament-modules

Tools to integrate Filament PHP support into modular Laravel applications using the nwidart/laravel-modules ecosystem. Provides artisan commands to install Filament support, create resources, pages, widgets, clusters, panels, plugins, and themes within specific modules.

Tokens
5.6K
Snippets
23
Records
26
Agent score
52%

What's inside Filament Modules

  1. Register the ModulesPlugin in a Filament Panel

    5.x

    To use the module management features, register the ModulesPlugin within your Filament Panel provider using the plugin() method.

    public function panel(Panel $panel): Panel
    {
        return $panel
            ->default()
            ->id('app')
            // ... other configuration
            ->plugin(\Coolsam\Modules\ModulesPlugin::make());
    }
    $panel->plugin(\Coolsam\Modules\ModulesPlugin::make());
  2. Configure ModulesPlugin for Filament

    5.x

    The ModulesPlugin is used to register and manage module-based Filament components (plugins and panels) within a Filament application. It relies on several configuration keys in config/filament-modules.php to determine how modules are discovered and displayed.

    Key Configuration Options

    KeyDefaultDescription
    modebothControls whether plugins and panels are registered. Uses ConfigMode enum.
    auto-register-pluginsfalseIf true, the plugin will automatically scan module directories for *Plugin.php files and register them.
    clusters.enabledfalseEnables Filament clusters.
    clusters.use-top-navigationfalseIf clusters are enabled, determines if top navigation is used.
    panels.group'Modules'The name of the navigation group where module panels will appear.
    panels.group-iconHeroicon::OutlinedRectangleStackThe icon for the module navigation group.
    panels.group-sort0The sort order for the module navigation group.
    panels.open-in-new-tabfalseWhether module panel links should open in a new browser tab.
    modules.paths.modules'Modules'The base path where modules are located.
    modules.paths.app_folder'app'The application folder within the modules.

    Automatic Plugin Discovery

    When auto-register-plugins is set to true, the plugin searches for files matching the pattern: {modules.paths.modules}/{module_name}/{modules.paths.app_folder}/Filament/*Plugin.php.

  3. Retrieve Filament Clusters from a module

    5.x

    To find all Filament Cluster namespaces within a module, use getModuleClusters(string $moduleName).

    This method scans the module's Filament/Clusters directory for files matching the pattern */*Cluster.php and returns an array of their fully qualified class namespaces.

    $clusterNamespaces = $modules->getModuleClusters('my-module');
  4. Execute shell commands via Modules

    5.x

    The execCommand(string $command, ?Command $artisan = null) method allows you to run shell commands. If an Artisan command instance is provided, the output is piped to $artisan->info(); otherwise, it is echoed directly to the standard output.

    $modules->execCommand('php artisan module:make Blog');
  5. Get Filament Page component locations for a module

    5.x

    Use getModuleFilamentPageComponentLocation(string $moduleName, ?string $panelId = null, bool $forCluster = false) to determine where a module's Filament pages should be stored and namespaced. This is useful for scaffolding or locating custom components.

    It returns an array containing:

    • namespace: The PHP namespace for the component class.
    • path: The filesystem path to the Blade view.
    • viewNamespace: The Blade view namespace (the module's lower-case name).

    Behavior based on arguments:

    • No $panelId and $forCluster is false: Targets resources/views/filament/pages and Filament\Pages namespace.
    • With $panelId: Targets resources/views/filament/{panel-kebab-name} and Filament\{PanelStudlyName} namespace.
    • With $forCluster as true: Targets resources/views/filament/clusters and Filament\Clusters namespace.
    $location = $modules->getModuleFilamentPageComponentLocation('blog', 'admin');
    
    // Returns:
    // [
    //   'namespace' => 'Modules\\Blog\\Filament\\Admin',
    //   'path' => '/path/to/Modules/Blog/resources/views/filament/admin',
    //   'viewNamespace' => 'blog'
    // ]
  6. Identify the module name for a given file path

    5.x

    If you have a file path and need to know which module it belongs to, use findModuleNameForPath(string $path).

    The method traverses up the directory tree from the provided path until it finds a module.json file. It returns the name defined in that JSON file, or falls back to the directory name if the JSON is not an array.

    $moduleName = $modules->findModuleNameForPath('/path/to/Modules/Blog/src/Models/Post.php');
    // returns 'blog' or the name from module.json
  7. Use the FilamentModules facade to access module functionality

    5.x

    The FilamentModules facade provides a convenient proxy to the underlying Coolsam\Modules\Modules class. You can use this facade to interact with the core module management logic within your Laravel application without needing to resolve the Modules class manually from the service container.

    use Coolsam\Modules\Facades\FilamentModules;
    
    // Example usage (actual methods depend on the Modules class implementation)
    $modules = FilamentModules::someMethod();
  8. Retrieve Filament Panels associated with a module

    5.x

    You can retrieve all Filament Panel instances belonging to a specific module using getModulePanels(string $moduleName).

    The method scans the module's Providers/Filament directory for classes ending in *PanelProvider.php. It then filters the application's registered Filament panels to return only those whose IDs match the generated kebab-case pattern: {module-kebab-name}-{panel-provider-name}.

    /** @return Panel[] */
    $panels = $modules->getModulePanels('my-module');
  9. Configure the module:make:filament-page command

    5.x

    The module:make:filament-page command supports the following arguments and options:

    Argument/OptionDescription
    nameThe name of the page to create.
    --moduleThe name of the module where the page should be created. If omitted, you will be prompted to select from enabled modules.
    --panelThe ID of the Filament panel where the page will be registered. If omitted, the command will attempt to select a panel based on the module's configuration or prompt you to choose.

    Interactive Workflow:

    1. Module Selection: If --module is not provided, an interactive list of enabled modules is shown.
    2. Panel Selection: If --panel is not provided, the command identifies available panels for the selected module. If the module has specific panels, you will be prompted to select one; otherwise, it defaults to the application's default panel.
    3. Cluster Selection: If the page is not part of a resource, the command will ask if you want to create the page within a cluster. If so, you can select from the clusters available in that module.
  10. Use module:make:filament-theme options and arguments

    5.x

    The module:make:filament-theme command accepts the following arguments and options:

    Argument/OptionDescription
    {module?}The name of the module where the theme should be created. If omitted, you will be prompted to enter a module name.
    --pm=Specify the package manager to use for installing dependencies (e.g., npm, yarn). Defaults to npm.
    --F or --forceForce the creation of files even if they already exist.

    Example usage with Yarn:

    php artisan module:make:filament-theme Blog --pm=yarn
    # Using npm (default)
    php artisan module:make:filament-theme Blog
    
    # Using yarn
    php artisan module:make:filament-theme Blog --pm=yarn
    
    # Overwriting existing files
    php artisan module:make:filament-theme Blog --force