Installation
composer require swisnl/filament-backgrounds
Publish the package assets (if customization is needed):
php artisan vendor:publish --provider="Swisnl\FilamentBackgrounds\FilamentBackgroundsServiceProvider" --tag="filament-backgrounds"
Configuration
Ensure your config/filament.php includes the package’s auth page customization:
'auth' => [
'pages' => [
'login' => [
'background' => true, // Enable backgrounds
],
],
],
First Use Case
Dynamic Backgrounds
The package injects a random background image from its curated list (resources/images) into Filament’s auth pages. Override this behavior by:
config/filament-backgrounds.php:
'images' => [
'path' => 'custom/path/to/images',
],
AuthPage hooks to dynamically select backgrounds:
use Swisnl\FilamentBackgrounds\Facades\FilamentBackgrounds;
Filament::serving(function () {
FilamentBackgrounds::setBackground('path/to/specific-image.jpg');
});
Theming Integration
resources/css/filament-backgrounds.css:
.filament-background {
background-size: cover;
opacity: 0.8; /* Example: Adjust opacity */
}
dark: variant in your CSS:
.filament-background.dark {
filter: brightness(0.7);
}
Localization & Accessibility
'fallback' => [
'color' => '#f3f4f6', // Default Filament gray
'gradient' => 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
],
'attributes' => [
'role' => 'img',
'aria-label' => __('filament-backgrounds::messages.background_image'),
],
Asset Paths
config/filament-backgrounds.php may break if the package is updated.FilamentBackgrounds::imagePath('custom-image.jpg');
Caching
<div style="background-image: url('{{ asset('images/background.jpg?t=' . time()) }}')">
Conflicts with Custom Auth Pages
AppServiceProvider:
public function boot()
{
Filament::auth()->middleware(function ($request) {
FilamentBackgrounds::applyBackground();
});
}
Log Background Selection Enable debug logging to verify which image is being loaded:
'debug' => env('FILAMENT_BACKGROUNDS_DEBUG', false),
Check logs for entries like:
[FilamentBackgrounds] Selected background: /path/to/image.jpg
Inspect Rendered HTML
Use browser dev tools to verify the background-image CSS property is applied:
.filament-background {
background-image: url(/vendor/swisnl/filament-backgrounds/images/...);
}
Custom Background Providers Create a custom provider to fetch backgrounds from an API or database:
use Swisnl\FilamentBackgrounds\Contracts\BackgroundProvider;
class ApiBackgroundProvider implements BackgroundProvider
{
public function getBackground(): string
{
return 'https://api.example.com/random-background';
}
}
Register it in config/filament-backgrounds.php:
'provider' => \App\Providers\ApiBackgroundProvider::class,
Event Listeners
Listen for the filament-backgrounds.selected event to log or modify the selected background:
FilamentBackgrounds::addListener(function (string $backgroundPath) {
Log::info("Custom logic for: {$backgroundPath}");
});
Testing Mock the background provider in tests:
$this->app->bind(
\Swisnl\FilamentBackgrounds\Contracts\BackgroundProvider::class,
function () {
return new class implements BackgroundProvider {
public function getBackground(): string
{
return 'tests/fixtures/background.jpg';
}
};
}
);
How can I help you explore Laravel packages today?