Laravel

Excluding FilamentPHP from API Requests

We show you how to prevent FilamentPHP from loading on API requests in Laravel projects that exclusively provide an API.

Why would you do this? Two reasons: it increases the stability and availability of your API and improves performance.

Chris
Managing Director, Senior PHP Developer
Exclude FilamentPHP from API requests.

How to Prevent FilamentPHP from Loading on an API Request

In the Filament AdminPanelProvider, add the following register() method, which ensures that the panel is not loaded on an API request:

class AdminPanelProvider extends PanelProvider
{
    public function register(): void
    {
        if (request()->is('api/*')) {
            return;
        }
    
        parent::register();
    }

    public function panel(Panel $panel): Panel
    {
        // dd('test');

        return $panel
            ->default()Language:php

Why Does This Make Sense?

Performance

If FilamentPHP is installed in the project, you can assume it is used for administration. It is not needed to handle the API requests and therefore does not have to be loaded. That saves resources. To illustrate this, I created 500 empty User resources and measured the response time:

  • With 500 Filament resources: ~107ms/request

  • Completely without Filament: ~19ms/request

Stability

We all make mistakes from time to time. Should an error sneak into the system that lies in the admin panel or one of its resources, we can thereby rule out that the availability of the API is restricted.