beier/filament-pages

针对filament的高度定制页面

v0.2.0 2022-11-14 23:13 UTC

README

Screenshot

beier/filament-pages

Latest Version on Packagist GitHub Tests Action Status Total Downloads

创建页面资源和前端路由的起点,这是我们对Page 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技巧中了解更多关于这个一般想法的细节。

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

要创建您自己的模板,请实现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

变更日志

有关最近更改的更多信息,请参阅变更日志

贡献

有关详细信息,请参阅贡献指南

安全漏洞

有关如何报告安全漏洞的信息,请参阅我们的安全策略

致谢

许可证

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