Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Sturm's avatar
Level 5

How to remove TrimStrings from an endpoint in Laravel 11

TL;DR

Laravel 11 now has the TrimStrings middleware built into the framework.

We have an endpoint that cannot use this middleware.

How can we "turn off" the TrimStrings middleware on this endpoint?

More Info

We're trying to use the Twilio verification system to verify that all incoming messages are, indeed, from Twilio. They don't support using an API key, weirdly, so we have to use their SDK's verification ability.

However, this verification ability requires that the whitespace be left intact to work.

Since there is no more app/Http/Kernel.php file from which to remove the TrimStrings middleware, I do not know how to remove it anymore.

0 likes
6 replies
LaryAI's avatar
Level 58

In Laravel 11, the TrimStrings middleware is built into the framework and is applied globally. To disable it for a specific endpoint, you can use route-specific middleware groups to exclude the TrimStrings middleware.

Here's how you can achieve this:

  1. Create a Middleware Group Without TrimStrings:

    First, you need to create a middleware group that does not include the TrimStrings middleware. You can do this in the app/Providers/RouteServiceProvider.php file.

    // app/Providers/RouteServiceProvider.php
    
    use Illuminate\Support\Facades\Route;
    use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
    
    class RouteServiceProvider extends ServiceProvider
    {
        public function boot()
        {
            parent::boot();
    
            // Define a middleware group without TrimStrings
            Route::middlewareGroup('no-trim', [
                // Add other middlewares that you want to keep
                \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
                // Add any other middleware you need
            ]);
        }
    
        public function map()
        {
            $this->mapApiRoutes();
            $this->mapWebRoutes();
        }
    
        protected function mapApiRoutes()
        {
            Route::prefix('api')
                 ->middleware('api')
                 ->namespace($this->namespace)
                 ->group(base_path('routes/api.php'));
        }
    
        protected function mapWebRoutes()
        {
            Route::middleware('web')
                 ->namespace($this->namespace)
                 ->group(base_path('routes/web.php'));
        }
    }
    
  2. Apply the Middleware Group to Your Specific Route:

    Next, apply the newly created middleware group to the specific route that should not use the TrimStrings middleware. You can do this in your routes/web.php or routes/api.php file.

    // routes/web.php or routes/api.php
    
    use Illuminate\Support\Facades\Route;
    
    Route::middleware('no-trim')->post('/your-endpoint', [YourController::class, 'yourMethod']);
    

By following these steps, you create a custom middleware group that excludes the TrimStrings middleware and apply it to the specific endpoint that requires whitespace to be preserved. This way, you can ensure that the TrimStrings middleware does not interfere with the Twilio verification process.

ruslansuhar's avatar

I've found this solution:

\Illuminate\Foundation\Http\Middleware\TrimStrings::skipWhen(function ($request) {
		// A condition based on the request
		return true;
});
maghajan's avatar

To remove all the default/global middleware that Laravel for some reason believes should be run by default... ) ->withMiddleware(function (Middleware $middleware) { $middleware->group('web', []); // Had to add this to prevent all the default middleware Laravel runs globally, such as vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php }) in the bootstrap/app.php file.

I understand that global middleware is useful, but having it run by default seems like it infringing on the "application".

eszterczotter's avatar

Sounds like you just want to remove TrimStrings from a single endpoint, in this case, I think this is the simplest option:

Route::get('/path', function () {})->withoutMiddleware([TrimStrings::class]);

See detailed docs here: https://laravel.com/docs/11.x/middleware#excluding-middleware

I suggest to leave it on for the rest of your application. It is in Laravel for a reason.

If I am mistaken and you need a more complex logic, @ruslansuhar's answer is the way to go, but then don't just return true from the closure, but actually determine if you must not run the middleware based on the request.

Lumethys's avatar

@eszterczotter ->withoutMiddleware() does not work for global middleware, it's even stated in the doc page you linked

2 likes
zackAJ's avatar

@Lumethys any clues on how to remove global middleware on specific routes ? or maybe register a global middleware while excluding some routes ?

Please or to participate in this conversation.