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.
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:
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'));
}
}
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.
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".
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.