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

bart's avatar
Level 13

Excluding middleware csrf

Since the latest update the CSRF middleware is part of the core and I'm not able to disable it anymore.

I want to send a simple AJAX post to a controller but getting a TokenMismatch exception. Any idea if I can EXCLUDE routes from using middleware by annotation? Something like @Middleware(exclude="csrf")?

0 likes
9 replies
az_iar's avatar

Comment the line in app/Http/Kernel.php 'App\Http\Middleware\VerifyCsrfToken',

bart's avatar
Level 13

Ah all initial middlewares have been moved there, thank you @az_iar! In general it is a good idea using this middleware as default in every post request but for an API controller or sth it won't work. So having the option to remove it globally and using it explicit is a great solution.

afrayedknot's avatar

As a side point - there is normally no need to disable CSRF for Ajax. You can easily include the token in your Ajax form, and pass it along with the request.

By disabling the CSRF check for Ajax - you open a small risk of a CSRF attack relating to that function.

bart's avatar
Level 13

@theshiftexchange tried it by generating the token using csrf_token() and submitting it as field "_token". But still getting the exception. What did I wrong?

Btw: I'm not a big fan of disabling it, too. So getting it run with sending the token would be the best solution.

afrayedknot's avatar

It is a bit hard to tell without seeing your code - but you need something like this in your form:

   <input type="hidden" name="_token" value="{{ csrf_token() }}">

Then in your ajax function something like this:

 $(document).ready( function() {
       var form = $('#my_awesome_form');
       $.ajax({
                        url: form[0].action,
                        type: form[0].method,
                        data: form.serialize(),
                        dataType: 'json',
                        success: function(data)
                        {
                            alert('yay');
                         }
               });

If it is still failing - you'll need to do some local debugging. Check your POST and make sure the _token is there. etc

2 likes
bart's avatar
Level 13

It's working perfect! Did the mistake creating multiple CSRF tokens which resulted in the exception. Everything is fine now, with CSRF middleware in AJAX requests!

1 like
raultm's avatar

I know the topic is 5-months-old but today I struggle with the same problem.

I wanted to avoid only the API calls so comment the middleware was not an option. I decide to bypass the CSRF in the API calls like that

Disclaimer: All the routes of the API starts with ENDPOINT/api/...

class VerifyCsrfToken extends BaseVerifier {

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(strpos($request->getRequestUri(), 'api') >= 0)
        {
            return $next($request);
        }
        return parent::handle($request, $next);
    }

}
2 likes

Please or to participate in this conversation.