公告: 旧版文档请移步 -> http://v2.laravel-filament.cn/docs

Languages

Version

Theme

面板构建器

插件开发

概述

Filament 插件的基础是 Laravel 包。它们通过 Composer 安装到 Filament 项目中,并遵循所有标准技术,如使用服务提供商注册路线、视图和翻译。如果你是 Laravel 软件包开发的新手,这里有一些资源可以帮助你掌握核心概念:

Filament 插件是基于 Laravel 包的概念构建的,允许你为任何 Filament 面板提供和使用可重复使用的功能。它们可以一次添加到每个面板中,每个面板也可以进行不同的配置。

使用插件类配置面板

Plugin 插件类允许你的包与面板配置 文件进行互动。它是一个实现 Plugin 接口的简单 PHP 类。3 个方法是必须的:

  • getId() 方法返回该插件的唯一标识符。请确保它足够具体,不会与同一项目中可能使用的其他插件发生冲突。
  • register() 方法允许你使用面板可用的配置选项。包括注册资源页面主题渲染钩子等到。
  • 只有当面板中注册的插件被实际使用时才会运行。它由中间件类执行。
<?php
 
namespace DanHarrin\FilamentBlog;
 
use DanHarrin\FilamentBlog\Pages\Settings;
use DanHarrin\FilamentBlog\Resources\CategoryResource;
use DanHarrin\FilamentBlog\Resources\PostResource;
use Filament\Contracts\Plugin;
use Filament\Panel;
 
class BlogPlugin implements Plugin
{
public function getId(): string
{
return 'blog';
}
 
public function register(Panel $panel): void
{
$panel
->resources([
PostResource::class,
CategoryResource::class,
])
->pages([
Settings::class,
]);
}
 
public function boot(Panel $panel): void
{
//
}
}

插件用户可以通过实例化插件类,并将其传入到配置plugin() 方法中,来将其加入面板中。

use DanHarrin\FilamentBlog\BlogPlugin;
 
public function panel(Panel $panel): Panel
{
return $panel
// ...
->plugin(new BlogPlugin());
}

流式实例化插件类

你可能想将 make() 方法加入到插件类,以提供 Fluent 接口,供用户实例化。另外,使用容器(app())来实例化插件对象,它在运行时使用不同的实现替换。

use Filament\Contracts\Plugin;
 
class BlogPlugin implements Plugin
{
public static function make(): static
{
return app(static::class);
}
// ...
}

现在,插件用户就可以使用 make() 方法:

use DanHarrin\FilamentBlog\BlogPlugin;
use Filament\Panel;
 
public function panel(Panel $panel): Panel
{
return $panel
// ...
->plugin(BlogPlugin::make());
}

按照面板配置插件

你可以将其他方法添加到插件类,以允许你的用户对其进行配置。我们建议为你提供每个选项同时添加 setter 和 getter 方法。你可以使用一个属性来存储 setter 中的偏好,并在 getter 对其进行中检索:

use DanHarrin\FilamentBlog\Resources\AuthorResource;
use Filament\Contracts\Plugin;
use Filament\Panel;
 
class BlogPlugin implements Plugin
{
protected bool $hasAuthorResource = false;
public function authorResource(bool $condition = true): static
{
// This is the setter method, where the user's preference is
// stored in a property on the plugin object.
$this->hasAuthorResource = $condition;
// The plugin object is returned from the setter method to
// allow fluent chaining of configuration options.
return $this;
}
public function hasAuthorResource(): bool
{
// This is the getter method, where the user's preference
// is retrieved from the plugin property.
return $this->hasAuthorResource;
}
public function register(Panel $panel): void
{
// Since the `register()` method is executed after the user
// configures the plugin, you can access any of their
// preferences inside it.
if ($this->hasAuthorResource()) {
// Here, we only register the author resource on the
// panel if the user has requested it.
$panel->resources([
AuthorResource::class,
]);
}
}
// ...
}

此外,你可以使用插件的唯一 ID,来访问插件类外部的任何配置选项。为此,请将 ID 传入到 filament() 方法中:

filament('blog')->hasAuthorResource()

你可能希望在访问配置时,获得更好的类型安全及 IDE 补全。具体如何实现完全由你决定,不过其中一个方式是,添加一个静态方法到插件类中来检索它:

use Filament\Contracts\Plugin;
 
class BlogPlugin implements Plugin
{
public static function get(): static
{
return filament(app(static::class)->getId());
}
// ...
}

现在,你可以使用新的静态方法访问插件配置:

BlogPlugin::get()->hasAuthorResource()

用插件的方式发布面板

以 Laravel 包的方式发布整个面板很简单。通过这种方法,用户可以轻松安装插件并拥有整个预制应用。

配置面板时,配置类继承了 PanelProvider 类,且它是一个标准的 Laravel 服务提供者。你可以在包中以服务提供者的方式使用它:

<?php
 
namespace DanHarrin\FilamentBlog;
 
use Filament\Panel;
use Filament\PanelProvider;
 
class BlogPanelProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
->id('blog')
->path('blog')
->resources([
// ...
])
->pages([
// ...
])
->widgets([
// ...
])
->middleware([
// ...
])
->authMiddleware([
// ...
]);
}
}

接下来你应该在包的 composer.json 中将其注册为服务提供者:

"extra": {
"laravel": {
"providers": [
"DanHarrin\\FilamentBlog\\BlogPanelProvider"
]
}
}
Edit on GitHub

Still need help? Join our Discord community or open a GitHub discussion

上一页
主题
下一页
测试