artificertech/filament-multi-context

此包已被弃用且不再维护。作者建议使用 filament v3 包代替。

为 filament 管理面板添加多个上下文的包

V2.1.2 2023-05-15 00:09 UTC

README

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

此包允许您在应用程序中注册多个 filament 上下文,每个上下文都有自己的一套资源和页面

安装

您可以通过 composer 安装此包

composer require artificertech/filament-multi-context

用法

使用以下命令创建一个新的 filament 上下文

php artisan make:filament-context FilamentTeams

上述命令将创建以下文件和目录

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

Filament 不能作为上下文传递给此命令,因为它是为默认 filament 安装保留的

注册提供者:

请确保将 FilamentTeamsServiceProvider 类添加到 config/app.php 中的 providers 数组中

添加资源和页面

现在您可以在 FilamentTeams 目录中添加 filament 资源。

上下文特性:请确保将 ContextualPage 和 ContextualResource 特性添加到上下文目录中相关联的类中。(我在 v2 版本中非常努力地尝试使此步骤变得不必要,但不幸的是,我们还是到了这里)。如果没有这些,当 filament 生成导航链接时,它将尝试使用 filament.pages.*filament.resources.{resource}.* 而不是 {context}.pages.*{context}.resources.{resource}.* 作为路由名称

ContextualPage & ContextualResource 特性

页面

namespace App\FilamentTeams\Pages;

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

class Dashboard extends Page
{
    use ContextualPage;
}

资源

namespace App\FilamentTeams\Resources;

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

class UserResource extends Resource
{
    use ContextualResource;
}

配置

config/filament-teams.php 文件包含 config/filament.php 配置文件的子集。可以在 filament-teams.php 文件中调整值,并且这些值将仅影响 filament-teams 上下文的页面、资源和小部件。

目前可以修改特定上下文的配置值包括

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

ContextServiceProvider

您在 app/Providers/FilamentTeamsServiceProvider.php 中找到的 ContextServiceProvider 是 Filament PluginServiceProvder 的扩展,因此可以为您上下文使用 PluginServiceProvider 的功能。添加到提供者中的任何 UserMenuItems、脚本、样式或 scriptData 将仅为此上下文注册

自定义页面和资源路由

如果您希望对页面和资源的路由方式有更多控制,您可以在 FilamentTeamsServiceProvider 中覆盖 componentRoutes() 函数

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

更改上下文守卫

默认情况下,所有上下文都将使用在主要的 filament.php 配置文件中定义的守卫。但是,如果您需要为特定上下文指定守卫,您可以将以下行添加到您的上下文配置文件中

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

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

!!! 纤维外观

为了使此包正常工作,已覆盖了filament应用程序服务。每个上下文都由其自己的Filament\FilamentManager对象表示。在您的应用程序中,对纤维外观的调用(例如Filament::serving)将被代理到适当的Filament\FilamentManager对象,该对象基于您的应用程序的当前上下文(由请求的路由确定)

上下文函数

以下函数已添加,以方便在您的应用程序中存在多个Filament\FilamentManger对象

// 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)

测试

composer test

变更日志

请参阅变更日志以获取有关最近更改的更多信息。

贡献

请参阅贡献指南以获取详细信息。

安全漏洞

请查阅我们的安全策略了解如何报告安全漏洞。

鸣谢

许可协议

MIT 许可协议(MIT)。请参阅许可文件以获取更多信息。