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

bytefury's avatar

CSRF filter on all post requests except one?

i have this defined in routes file. Route::when('*','csrf',['post']);

Now, how can i disable csrf on a particular post route?

0 likes
4 replies
pmall's avatar

You have to find a pattern that match every routes except this particular route. Anyway, why do you want to disable csrf protection on one route ?

bytefury's avatar

i am trying to get values from a payment gateway which sends a confirmation after the payment is complete. but the problem is that it sends a post request on a specified link .

bashy's avatar
bashy
Best Answer
Level 65

Not sure if there's an ->except() for a Route::when() but you could try this. What about making a new one to filter out certain? Or of course add it onto the original csrf filter.

Route::filter('csrf2', function()
{
    if ( ! Request::is('payment-return/*'))
    {
        if (Session::token() !== Input::get('_token'))
        {
            throw new Illuminate\Session\TokenMismatchException;
        }
    }
});

Route::when('*', 'csrf2', ['post']);
1 like

Please or to participate in this conversation.