Skip to content

RedirectHome Trait

The RedirectHome trait provides a simple, standardized way to handle redirects back to a resource's index page in Filament resources and pages.

Features

This trait offers:

  • Consistent redirect behavior across resources
  • Simple implementation for common redirect patterns
  • Integration with Filament's URL generation

Methods

getRedirectUrl(): string

Returns the URL to redirect to, typically after completing an action.

  • Returns:
    • The URL of the resource's index page
  • Behavior:
    • Uses the resource's static getUrl method with 'index' as the parameter
    • Resolves to the listing/index page of the current resource

Usage Example

php
<?php

namespace Modules\Blog\Filament\Resources\PostResource\Pages;

use Filament\Resources\Pages\CreateRecord;
use Modules\Blog\Filament\Resources\PostResource;
use ModuleManager\ModuleManager\Concerns\RedirectHome;

class CreatePost extends CreateRecord
{
    use RedirectHome;

    protected static string $resource = PostResource::class;

    // No need to define getRedirectUrl() - it will redirect to the posts index
}

Integration

This trait is commonly used in:

  • Create record pages
  • Edit record pages
  • Custom action pages
  • Any page where you want to return to the resource listing after an action

Notes

  • This trait simplifies the common pattern of redirecting back to a resource listing
  • It provides consistent redirect behavior across all resources in your application
  • The trait works with all Filament resource types