Laravel Filament is primarily known as a server-driven UI (SDUI) framework for building admin panels, dashboards, and internal tools within Laravel applications, but it can also be used for the front end to create user-facing interfaces. Filament is built on top of Laravel Livewire, Alpine.js, and Tailwind CSS, offering a modern and reactive developer experience without the need to write custom JavaScript.
Server-Driven UI Concept in Filament
Filament operates on the SDUI principle, which means the server controls the UI dynamically through PHP classes and structured configuration objects rather than relying on static templates. This approach allows for greater consistency, centralized logic, and rapid iteration on interfaces across both admin and frontend applications. Unlike traditional server-rendered UIs where views are defined upfront, the SDUI implemented by Filament dynamically generates the UI based on real-time data and business needs on the backend, enabling flexible and reactive user experiences.
Using Filament Components in Frontend
Although Filament is often associated with admin panels, its components can be used on public-facing pages of a Laravel application. To use Filament in the frontend, several setup steps are required:
1. Include Filament Styles and Scripts
Frontend Blade templates need to include Filament's styles and scripts using special Blade directives. For example, within a Blade layout file (e.g., `layouts/app.blade.php`), you add the following directives to load Filament's CSS and JavaScript assets:
@filamentStyles
@filamentScripts
This ensures that all necessary Tailwind CSS styles and Livewire scripts for Filament components are loaded correctly on the frontend.
2. Configure Tailwind CSS
Since Filament uses Tailwind CSS for its styling, the Tailwind configuration must include Filament's preset. This is done by importing Filament's Tailwind preset into the `tailwind.config.js` file and extending the content paths to include Filament's Blade components:
js
import preset from './vendor/filament/filament/tailwind.config.preset';
export default {
content: [
'./resources/**/*.blade.php',
'./vendor/filament/**/*.blade.php',
],
presets: [preset],
};
After this configuration, rebuild the CSS bundle to incorporate Filament styles.
3. Register Filament Colors Globally
For consistent theming, you can register Filament colors globally in the application service provider (`AppServiceProvider`) using the Filament color facade. This setup centralizes style themes available in both frontend and admin panels.
4. Use Filament Blade Components
Filament provides a range of reusable Blade components such as buttons, sections, cards, inputs, and more. These components can be used in any Blade view to build interactive UI elements. For example:
blade
Click Me
Section Heading
Section Description
Content here
This allows frontend pages to leverage the same UI components as the admin area for a cohesive design system.
Integrating Filament Components on Custom Frontend Pages
Filament components are powered by Livewire, so they operate reactively without custom JavaScript coding. Developers can embed Filament tables, forms, and widgets into custom frontend pages, even using tables to display data dynamically with features such as sorting, filtering, and pagination. This is useful when frontend user interfaces require interaction with application data beyond simple static views.
Custom pages can be created in Laravel routes or controllers, rendering Blade views that include Livewire components and Filament UI elements. Filament's structure and reusable components facilitate building complex, data-driven UIs with minimal frontend configuration.
Using Filament for Both Frontend and Admin
It is possible to use Filament to handle both the user-facing frontend and admin backend within a Laravel application. This often involves:
- Defining separate route groups and middleware for frontend and admin areas to enforce security and role-based access.
- Using custom layouts suited to either user-facing or admin-facing interfaces.
- Managing authentication states through a central Laravel authentication system but applying different guards or permission checks based on user roles.
- Sharing component libraries and UI patterns across frontend and admin for design consistency.
While Filament is primarily tailored for admin interfaces, these capabilities allow it to be extended as a unified solution covering multiple application layers, reducing the need for separate frontend frameworks.
Advantages of Using Filament in Frontend Development
- Consistency: Using the same UI components for frontend and backend ensures a unified look and feel.
- Rapid Development: Filament's schema-driven and PHP-based component definition accelerates UI development without needing to write React or Vue code.
- Reactivity: With Livewire integration, Filament components update dynamically, providing a modern user experience.
- Reduced Frontend Complexity: Developers can build complex interfaces like forms, tables, and modals solely using PHP and Blade components.
- Tailwind CSS Integration: Filament's style components align with Tailwind CSS utility-first approach, facilitating custom theming.
Limitations and Considerations
- Filament is designed with admin panels in mind, so some aspects might not fit traditional frontend UI patterns without careful customization.
- Performance considerations on the frontend need to be addressed, as Livewire relies on backend round-trips for interactivity.
- Large-scale frontend interactions requiring complex client-side logic may require additional JavaScript or dedicated SPA frameworks.
- Separation of concerns for frontend and admin may require thoughtful routing, layout, and middleware architecture.
Practical Example Workflow
A typical workflow using Filament on the frontend would include:
- Setup: Install Filament, configure Tailwind CSS, and set up Blade layouts with filament styles/scripts.
- Component Use: Embed Filament components like buttons, forms, or tables inside frontend Blade views.
- Data Binding: Use Laravel controllers or Livewire components to bind data models to UI elements.
- Interactivity: Handle events such as form submissions, data filtering, or modal dialogs with Livewire-powered Filament actions.
- Access Control: Use Laravel's middleware and authorization features to protect frontend routes and data operations.
- Theming: Customize colors and styles globally using Filament's color management and Tailwind presets.
Ecosystem and Packages
Filament offers a modular ecosystem of packages that can be selectively used on frontend projects:
- filament/forms: For creating dynamic, validated form inputs.
- filament/tables: To display interactive tables with paginations, filters.
- filament/actions: To provide button-triggered modals and workflows.
- filament/notifications: For in-browser notifications and alerts.
- filament/widgets: To create dashboard-style component widgets on user pages.
- filament/schemas: Underlying UI building blocks to define component layouts programmatically.
These components can be integrated flexibly into Laravel's routes and views for both backend administration and frontend user interaction.
Community and Best Practices
The Filament community shares examples and discussions on using Filament in versatile ways, including frontend usage. Best practices include:
- Keeping frontend and admin routes clearly separated.
- Utilizing Laravel middleware for role-based access control.
- Leveraging Filament's Blade and Livewire components to build reusable UI kits.
- Extending Filament with custom components when needed.
- Profiling and caching Livewire interactions for optimal frontend performance.
Using Filament beyond admin panels opens new possibilities for Laravel developers to maintain a single framework for multiple UI needs, simplifying development and maintenance workflows.
In summary, Laravel Filament can be used in frontend development as a powerful framework for building reactive, consistent, and easily maintainable user interfaces within Laravel. By incorporating the necessary styling, scripts, and Tailwind configurations, Filament components can be embedded into public-facing pages, allowing developers to create interactive forms, tables, and UI elements without custom JavaScript. This approach embraces the server-driven UI paradigm, providing flexibility and centralized control over both frontend and admin interfaces while leveraging Laravel's authentication, authorization, and routing features for a seamless application experience.