Filament Fabricator

repository·4.x·Indexed 18 days ago

https://github.com/z3d0x/filament-fabricator

A block-based page builder skeleton for Filament applications. It manages PageResource and frontend routing, enabling developers to create custom Layouts and Page Blocks. Features include automatic routing with customizable prefixes, route caching, and a flexible schema for defining block data and mutations.

Tokens
7.8K
Snippets
37
Records
44
Agent score
64%

What's inside filament-fabricator

  1. Create a Page Block

    4.x

    Page Blocks are the building blocks of your pages. Generate a new block using:

    php artisan filament-fabricator:block MyBlock

    This creates a class extending Z3d0X\FilamentFabricator\PageBlocks\PageBlock. You define the block's structure in defineBlock() and can transform data in mutateData().

  2. Upgrade Filament from v4 to v5

    4.x

    To upgrade your project from Filament v4 to v5, first follow the official Filament upgrade guide. If additional steps are required, use the following commands:

    1. Install the upgrade tool:
      composer require filament/upgrade:"^5.0" -W --dev
    2. Run the upgrade script:
      ./vendor/bin/filament-v5
    3. Run the specific commands output by the script (these are unique to your application, but typically involve updating filament and filament-fabricator to ^5.0).
    composer require filament/upgrade:"^5.0" -W --dev
    ./vendor/bin/filament-v5
    # Followed by unique commands from the script output
  3. Migrate Filament Fabricator from 2.x to 3.x

    4.x

    Upgrading from 2.x to 3.x involves the following changes:

    • The pages.layout database column no longer has a default value.
    • The FilamentFabricatorManager#getPageUrlFromId method no longer accepts a prefixSlash parameter.
    • Requires PHP 8.2 as the minimum version.
  4. Install Filament Fabricator

    4.x

    To install Filament Fabricator, ensure you have Filament Panels configured, then follow these steps:

    1. Install the package via Composer:
      composer require z3d0x/filament-fabricator
    2. Run the installation command to publish configuration and migrations:
      php artisan filament-fabricator:install
    3. Register the FilamentFabricatorPlugin in your Panel provider:
      use Z3d0Xilament-fabricator\
      FilamentFabricatorPlugin;
      
      public function panel(Panel $panel): Panel
      {
          return $panel
              // ...
              ->plugins([
                  FilamentFabricatorPlugin::make(),
              ]);
      }
    4. Publish the plugin assets:
      php artisan filament:assets
    composer require z3d0x/filament-fabricator
    php artisan filament-fabricator:install
    php artisan filament:assets
  5. Upgrade Filament from v3 to v4

    4.x

    To upgrade your project from Filament v3 to v4, first follow the official Filament upgrade guide. If additional steps are required, use the following commands:

    1. Install the upgrade tool:
      composer require filament/upgrade:"^4.0" -W --dev
    2. Run the upgrade script:
      ./vendor/bin/filament-v4
    3. Run the specific commands output by the script (these are unique to your application, but typically involve updating filament and filament-fabricator to ^4.0).
    composer require filament/upgrade:"^4.0" -W --dev
    ./vendor/bin/filament-v4
    # Followed by unique commands from the script output
  6. Create a new Layout

    4.x

    Use the Artisan command to generate a new Layout class and its corresponding Blade component:

    php artisan filament-fabricator:layout DefaultLayout

    This creates a class extending Z3d0X\FilamentFabricator\Layouts\Layout. In the generated Blade component, you must use the filament-fabricator::page-blocks component to render the page content:

    @props(['page'])
    <x-filament-fabricator::layouts.base :title="$page->title">
        {{-- Custom Header --}}
        <x-filament-fabricator::page-blocks :blocks="$page->blocks" />
        {{-- Custom Footer --}}
    </x-filament-fabricator::layouts.base>

    Note: You can use the $page instance within your layout to build custom logic.

  7. Register the FilamentFabricatorPlugin

    4.x

    After installation, you must register the FilamentFabricatorPlugin within your Filament Panel provider's panel method to enable the page builder functionality.

    use Z3d0X\ilament-fabricator\FilamentFabricatorPlugin;
    
    //..
    
    public function panel(Panel $panel): Panel
    {
        return $panel
            // ...
            ->plugins([
                FilamentFabricatorPlugin::make(),
            ]);
    }
  8. Configure Laravel lifecycle hooks for route caching

    4.x

    By default, Filament Fabricator automatically clears or refreshes its routes cache when you run standard Laravel optimization commands.

    Supported commands that trigger cache management:

    • cache:clear (clears)
    • config:cache (refreshes)
    • config:clear (clears)
    • optimize (refreshes)
    • optimize:clear (clears)
    • route:clear (clears)

    To opt out of this automatic behavior, set the hook-to-commands config option to false in your configuration file.

  9. Configure Auto-routing and Route Prefixes

    4.x

    Filament Fabricator provides automatic routing for your pages via a fallback route. You can customize this behavior in your configuration file:

    • Disable Auto-routing: Set routing.enabled to false if you want manual control over page rendering.
    • Add a Route Prefix: Use the prefix configuration to prepend a string to all page slugs. For example, a prefix of /pages and a slug of page-1 results in the URL /pages/page-1.

    Important: If you change the route prefix, you must clear the routes cache using the Artisan command provided below.

    // Example configuration logic
    'routing' => [
        'enabled' => true,
        'prefix' => '/pages',
    ],
  10. Register the Filament Fabricator plugin

    4.x

    To use Filament Fabricator in your Filament panel, register the FilamentFabricatorPlugin using the make() method within your panel configuration. The plugin automatically registers its required resources and ensures FilamentPeekPlugin is available.

    use Z3d0X\FilamentFabricator\FilamentFabricatorPlugin;
    
    public function panel(Panel $panel): Panel
    {
        return $panel
            ->plugins([
                FilamentFabricatorPlugin::make(),
            ]);
    }
  11. Define Page Block schema and data mutation

    4.x

    In your PageBlock class, you control how data is structured and passed to the Blade view.

    Defining the Schema

    Use defineBlock(Block $block) to define the fields. You can use standard Filament fields and even conditionally show blocks based on the current layout:

    public static function defineBlock(Block $block): Block
    {
        return $block->schema([
            TextInput::make('name'),
        ]);
    }

    Mutating Data

    By default, Blade props match the field names. Use mutateData(array $data) to transform the raw data before it reaches the view:

    public static function mutateData(array $data): array
    {
        return ['foo' => 'bar'];
    }

    In your Blade component, you would then access this via @dump($foo).

    public static function defineBlock(Block $block): Block
    {
        return $block->schema([
            TextInput::make('name'),
        ]);
    }
    
    public static function mutateData(array $data): array
    {
        return ['foo' => 'bar'];
    }