Giter Club home page Giter Club logo

filament-multi-context's Introduction

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

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

Resources:

namespace App\FilamentTeams\Resources;

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

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());
                }
            });
        };
    }

!!! 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.

filament-multi-context's People

Contributors

coleshirley avatar dependabot[bot] avatar github-actions[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.