featherwebs/filament-pages

为丝束提供高度可定制的页面。由 beier/filament-pages 衍生而来

dev-main 2024-06-26 10:54 UTC

This package is auto-updated.

Last update: 2024-09-26 11:16:56 UTC


README

Screenshot

beier/filament-pages

Latest Version on Packagist GitHub Tests Action Status Total Downloads

创建页面资源和前端路由的起点。这是我们对页面 CMS 功能进行打包的结果。

此包不注册任何路由,因为页面路由始终取决于您的项目。这旨在为您提供一个快速入门来创建动态页面模块。

演示

Demo

截图

Screenshot of list view Screenshot of resource view

安装

您可以通过 composer 安装此包

composer require beier/filament-pages

您可以使用以下命令运行安装命令

php artisan filament-pages:install

这是发布配置文件的内容

<?php

use Beier\FilamentPages\Filament\FilamentPageTemplates\DefaultTemplate;
use Beier\FilamentPages\Filament\Resources\FilamentPageResource;
use Beier\FilamentPages\Models\FilamentPage;
use Beier\FilamentPages\Renderer\SimplePageRenderer;

return [
    'filament' => [
        /*
        |--------------------------------------------------------------------------
        | Filament: Custom Filament Resource
        |--------------------------------------------------------------------------
        |
        | Use your own extension of the FilamentPageResource
        | below to fully customize every aspect of it.
        |
        */
        'resource' => FilamentPageResource::class,
        /*
        |--------------------------------------------------------------------------
        | Filament: Custom Filament Model
        |--------------------------------------------------------------------------
        |
        | Use your own extension of the FilamentPage Model
        | below to fully customize every aspect of it.
        |
        */
        'model' => FilamentPage::class,
        /*
        |--------------------------------------------------------------------------
        | Filament: Title Attribute
        |--------------------------------------------------------------------------
        |
        | Point to another field or Attribute to change the
        | computed record title provided in filament.
        |
        */
        'recordTitleAttribute' => 'title',
        /*
        |--------------------------------------------------------------------------
        | Filament: Label
        |--------------------------------------------------------------------------
        |
        | If you don't need to support multiple languages you can
        | globally change the model label below. If you do,
        | you should rather change the translation files.
        |
        */
        'modelLabel' => 'Page',
        /*
        |--------------------------------------------------------------------------
        | Filament: Plural Label
        |--------------------------------------------------------------------------
        |
        | If you don't need to support multiple languages you can
        | globally change the plural label below. If you do,
        | you should rather change the translation files.
        |
        */
        'pluralLabel' => 'Pages',
        'navigation' => [
            /*
            |--------------------------------------------------------------------------
            | Filament: Navigation Icon
            |--------------------------------------------------------------------------
            |
            | If you don't need to support multiple languages you can
            | globally change the navigation icon below. If you do,
            | you should rather change the translation files.
            |
            */
            'icon' => 'heroicon-o-document',
            /*
            |--------------------------------------------------------------------------
            | Filament: Navigation Group
            |--------------------------------------------------------------------------
            |
            | If you don't need to support multiple languages you can
            | globally change the navigation group below. If you do,
            | you should rather change the translation files.
            |
            */
            'group' => 'content',
            /*
            |--------------------------------------------------------------------------
            | Filament: Navigation Group
            |--------------------------------------------------------------------------
            |
            | If you don't need to support multiple languages you can
            | globally change the navigation sort below. If you do,
            | you should rather change the translation files.
            |
            */
            'sort' => null,
        ]
    ],
    /*
    |--------------------------------------------------------------------------
    | Templates
    |--------------------------------------------------------------------------
    |
    | Add your own Templates implementing FilamentPageTemplate::class
    | below. They will appear in the Template selection,
    | and persisted to the data column.
    |
    */
    'templates' => [
        DefaultTemplate::class,
    ],

    /*
    |--------------------------------------------------------------------------
    | Renderer
    |--------------------------------------------------------------------------
    |
    | If you want to use the Rendering functionality, you can create your 
    | own Renderer here. Take the available Renderers for reference.
    | See FilamentPageController for recommended usage.
    | 
    | Available Renderers:
    | - SimplePageRenderer: 
    |   Renders everything to the defined layout below.
    | - AtomicDesignPageRenderer: 
    |   More opinionated Renderer to be used with Atomic Design.
    |
    | To use the renderer, Add a Route for the exemplary FilamentPageController:
    | 
    |  Route::get('/{filamentPage}', [FilamentPageController::class, 'show']);
    |
    | To route the homepage, you could add a data.is_homepage 
    | field and query it in a controller.
    | 
    */
    'renderer' => SimplePageRenderer::class,

    /*
    |--------------------------------------------------------------------------
    | Simple Page Renderer: Default Layout
    |--------------------------------------------------------------------------
    |
    | Only applicable to the SimplePageRenderer.
    | 
    */
    'default_layout' => 'layouts.app',
];

使用方法

在 Filament 中

运行 安装命令 后,您将在 Filament 管理员中找到一个新页面资源。

模板

此包使用 Dennis Koch (pxlrbt) 提出的基于模板的表单的概念。您可以在他的 Filament Trick 中了解更多关于这个一般想法。

您将找到一个基本的页面模板。通过创建和选择您自己的模板,您能够完全自定义您的页面。

要创建自己的模板,实现 Beier\FilamentPages\Contracts\FilamentPageTemplate

<?php

namespace App\Filament\FilamentPageTemplates;

use Beier\FilamentPages\Contracts\FilamentPageTemplate;
use Filament\Forms\Components\Card;
use Filament\Forms\Components\RichEditor;

class MyTemplate implements FilamentPageTemplate
{
    public static function title(): string
    {
        return 'My Template';
    }

    public static function schema(): array
    {
        return [
            Card::make()
                ->schema([
                    RichEditor::make('content')
                ]),
        ];
    }
}

最后,在 config/filament-pages.php 中注册您的模板

<?php

return [
    'templates' => [
        // \Beier\FilamentPages\Filament\FilamentPageTemplates\DefaultTemplate::class,
        \App\Filament\FilamentPageTemplates\MyTemplate::class,
    ],
];

您的模板将出现在模板选择中,并相应地渲染模式。

自定义页面资源

扩展页面资源的推荐方法是覆盖

  • FilamentPageResource::insertBeforePrimaryColumnSchema
  • FilamentPageResource::insertAfterPrimaryColumnSchema
  • FilamentPageResource::insertBeforeSecondaryColumnSchema
  • FilamentPageResource::insertAfterSecondaryColumnSchema

在你的自己的 PageResource 类中,扩展 Beier\FilamentPages\Filament\Resources\FilamentPageResource

你将在配置文件中找到大多数常见的配置选项。

<?php

namespace App\Filament\Resources;

use Beier\FilamentPages\Filament\Resources\FilamentPageResource;
use Filament\Resources\Form;
use Filament\Resources\Table;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Components\TextInput;

class PageResource extends FilamentPageResource
{
    /**
     * Recommended: Insert Fields at the beginning of the primary column.
     */
    public static function insertBeforePrimaryColumnSchema(): array
    {

        return [
            Toggle::make('is_homepage'),
        ];
    }
    
    /**
     * Recommended: Insert Fields at the end of the primary column.
     */
    public static function insertAfterPrimaryColumnSchema(): array
    {

        return [
            TextInput::make('author'),
        ];
    }
    
    /**
     * Recommended: Insert Fields at the beginning of the secondary column. (sidebar)
     */
    public static function insertBeforeSecondaryColumnSchema(): array
    {

        return [
            Toggle::make('is_homepage'),
        ];
    }
    
    /**
     * Recommended: Insert Fields at the end of the secondary column. (sidebar)
     */
    public static function insertAfterSecondaryColumnSchema(): array
    {

        return [
            SEO::make(),
        ];
    }

    /**
     * Not Recommended: Override the whole form
     */
   /** public static function form(Form $form): Form
    {

        return $form
            ->schema([
                //
            ]);
    } */
    
    /**
     * Not Recommended: Override the whole table
     */
    /** public static function table(Table $table): Table
    {

        return $table
            ->columns([
                //
            ]);
    } */
}

然后,在 config/filament-pages.php 中注册你的类

<?php

use App\Filament\Resources\PageResource;

return [
    'filament' => [
        'resource' => PageResource::class,
    ],
]

示例

laravel-filament-seo 一起使用

  1. 创建 app/Models/FilamentPage.php

    <?php
    
    namespace App\Models;
    
    use Beier\FilamentPages\Models\FilamentPage as BeierFilamentPage;
    use RalphJSmit\Laravel\SEO\Support\HasSEO;
    use RalphJSmit\Laravel\SEO\Support\SEOData;
    
    class FilamentPage extends BeierFilamentPage
    {
        use HasSEO;
    
        public function getDynamicSEOData(): SEOData
        {
            return new SEOData(
                title: $this->title,
            );
        }
    
    }
  2. 创建 app/Filament/Resources/PageResource.php

    <?php
    
    namespace App\Filament\Resources;
    
    use RalphJSmit\Filament\SEO\SEO;
    use Beier\FilamentPages\Filament\Resources\FilamentPageResource;
    
    class PageResource extends FilamentPageResource
    {
        public static function insertAfterSecondaryColumnSchema(): array
        {
            return [
                SEO::make(),
            ];
        }
    }
  3. config/filament-pages.php 中定义模型和资源

    return [
        'filament' => [
             'resource' => \App\Filament\Resources\PageResource::class,
             'model' => \App\Models\FilamentPage::class,
        ],
    ];
  4. 如有必要,清除应用程序缓存以加载您的资源服务定位器

    $ php artisan cache:clear

测试

composer test

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

贡献

有关详细信息,请参阅 CONTRIBUTING

安全漏洞

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

致谢

许可

MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件