Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Latest commit

 

History

History
251 lines (187 loc) · 7.28 KB

README.md

File metadata and controls

251 lines (187 loc) · 7.28 KB

A package for adding multiple contexts to the filament admin panel

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package allows you to register multiple filament contexts in your application with their own set of resources and pages

Installation

You can install the package via composer:

composer require artificertech/filament-multi-context

Usage

create a new filament context using

php artisan make:filament-context FilamentTeams

The above command will create the following files and directories:

app/FilamentTeams/Pages/
app/FilamentTeams/Resources/
app/FilamentTeams/Widgets/
app/Providers/FilamentTeamsServiceProvider.php
config/filament-teams.php

Filament cannot be passed as a context to this command as it is reserved for the default filament installation

Register Provider:

Be sure to add the FilamentTeamsServiceProvider class to your providers array in config/app.php

Adding Resources and Pages

You may now add filament resources in your FilamentTeams directories.

Context Traits: be sure to add the ContextualPage and ContextualResource traits to their associated classes inside of your context directories. (I tried really hard with v2 to make this unnecessary but sadly here we are). Without this when filament generates navigation links it will try to use filament.pages.* and filament.resources.{resource}.* instead of {context}.pages.* and {context}.resources.{resource}.* as the route names

ContextualPage & ContextualResource traits

Pages:

namespace App\FilamentTeams\Pages;

use Artificertech\FilamentMultiContext\Concerns\ContextualPage;
use Filament\Pages\Page;

class Dashboard extends Page
{
    use ContextualPage;
}

Resources:

namespace App\FilamentTeams\Resources;

use Artificertech\FilamentMultiContext\Concerns\ContextualResource;
use Filament\Resources\Resource;

class UserResource extends Resource
{
    use ContextualResource;
}

Configuration

The config/filament-teams.php file contains a subset of the config/filament.php configuration file. The values in the filament-teams.php file can be adjusted and will only affect the pages, resources, and widgets for the filament-teams context.

Currently the configuration values that can be modified for a specific context are:

'path'
'domain'
'pages'
'resources'
'widgets'
'livewire'
'middleware'

ContextServiceProvider

Your ContextServiceProvider found in your app/Providers/FilamentTeamsServiceProvider.php is an extension of the Filament PluginServiceProvder so features of the PluginServiceProvider may be used for your context. Any UserMenuItems, scripts, styles, or scriptData added to the provider will be registered for that context only

Custom Page and Resource Routing

If you would like more control over the way pages and resources are routed you may override the componentRoutes() function in your FilamentTeamsServiceProvider

protected function componentRoutes(): callable
    {
        return function () {
            Route::name('pages.')->group(function (): void {
                foreach (Facades\Filament::getPages() as $page) {
                    Route::group([], $page::getRoutes());
                }
            });

            Route::name('resources.')->group(function (): void {
                foreach (Facades\Filament::getResources() as $resource) {
                    Route::group([], $resource::getRoutes());
                }
            });
        };
    }

Changing the context guard

By default all contexts will use the guard defined in the primary filament.php config file. However if you need to specify the guard for a specific context you may add the following lines to your context config file:

/*
    |--------------------------------------------------------------------------
    | Auth
    |--------------------------------------------------------------------------
    |
    | This is the configuration that Filament will use to handle authentication
    | into the admin panel.
    |
    */

    'auth' => [
        'guard' => 'my-custom-guard',
    ],

!!! The Filament Facade

In order for this package to work the filament app service has been overriden. Each context is represented by its own Filament\FilamentManager object. Within your application calls to the filament facade (such as Filament::serving) will be proxied to the appropriate Filament\FilamentManager object based on the current context of your application (which is determined by the route of the request)

Context Functions

The following functions have been added to facilitate multiple Filament\FilamentManger objects in your application:

// retrieve the string name of the current application context
// defaults to `filament`

Filament::currentContext(): string
// retrieve the Filament\FilamentManager object for the current app context

Filament::getContext()
// retrieve the array of Filament\FilamentManager objects keyed by the context name

Filament::getContexts()
// set the current app context.
// Passing null or nothing sets the context to 'filament'

Filament::setContext(string|null $context)
// sets the context for the duration of the callback function, then resets it back to the original value
Filament::forContext(string $context, function () {
    // ...
})
// loops through each registered context (including the default 'filament' context),
// sets that context as the current context,
// runs the callback, then resets to the original value
Filament::forAllContexts(function () {
    // ...
})
// creates a new FilamentManager object and registers it under the $name context
// this method is used by your ContextServiceProvider to register your context
// you shouldn't need to use this method during normal development
Filament::addContext(string $name)

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.