RicLeP's avatar
Level 6

TrimStrings still running when excluded from route

I am using Laravel 11. I need to exclude TrimStrings from a single route.

Globally this works in my bootstrap/app.php file:

 ->withMiddleware(function (Middleware $middleware) {
        $middleware->remove([
            TrimStrings::class,
            VerifyCsrfToken::class
        ]);
    })

However, I want to exclude the trimming from a single route - removing VerifyCsrfToken works, but the request is still being trimmed. It is expecting a JSON payload in the request and certain values are trimmed.

Route::post('/{slug?}', LiveContentController::class)
    ->where('slug', '(.*)')
    ->withoutMiddleware([
		TrimStrings::class,
		VerifyCsrfToken::class
    ])
    ->middleware([\Riclep\Editor\Http\Middleware\Editor::class]);

Any idea why I can’t remove the middleware directly on the route? Is there another way to exclude just this route?

Thanks

Ric

0 likes
1 reply
LaryAI's avatar
Level 58

In Laravel, middleware can be applied globally, to groups, or to individual routes. When you want to exclude a middleware from a specific route, using the withoutMiddleware method is the correct approach. However, if the middleware is still being applied, it might be due to the order of middleware execution or how the middleware is registered.

Here are a few steps to troubleshoot and resolve the issue:

  1. Check Middleware Registration: Ensure that TrimStrings is not being applied at a higher level, such as in a middleware group that your route is part of. If it's part of a group, you might need to adjust the group configuration.

  2. Order of Middleware: Make sure that the withoutMiddleware call is correctly placed and that no other middleware is re-adding TrimStrings after it's been removed.

  3. Custom Middleware: If the above steps don't resolve the issue, consider creating a custom middleware that conditionally applies TrimStrings based on the route or request attributes.

Here's an example of how you might create a custom middleware to conditionally apply TrimStrings:

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\TrimStrings as BaseTrimmer;

class ConditionalTrimStrings extends BaseTrimmer
{
    public function handle($request, \Closure $next)
    {
        // Check if the current route should skip trimming
        if ($request->is('your/specific/route')) {
            return $next($request);
        }

        // Otherwise, apply the trimming
        return parent::handle($request, $next);
    }
}
  1. Apply Custom Middleware: Replace the global TrimStrings middleware with your custom ConditionalTrimStrings middleware in app/Http/Kernel.php:
protected $middleware = [
    // Other middleware
    \App\Http\Middleware\ConditionalTrimStrings::class,
    // Other middleware
];

By using a custom middleware, you can have more granular control over when and where the trimming logic is applied. This should help you exclude the trimming from specific routes as needed.

Please or to participate in this conversation.