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

Polinicles's avatar

CSRF Token

Hi there!

I've just started a new app with Laravel 5.1 and I've found myself with many problems. I've created a simple registration-login system for the users and I used some Events + Jobs in my Controller to create the new user, send the registration e-mail, etc. BUT if I use the Events/Jobs I can't stop getting a TokenMismatchException in the VerifyCsrfToken.php.

As far as I know:

  • Laravel provides a CSRF protection by default in any Form POST request. If I dd() a normal $request (without adding the CSRF Token) I can see the token.
  • Laravel 5.1 has a global CSRF Middleware for all Requests.

So...

  1. Why do I have to put an extra token in my form? {!! csrf_field() !!}
  2. What is the correct use of this Middleware? Should I add it only when I'm sending a Form with a POST method?
  3. When should I exclude the CSRF Middleware?
  4. Why do I get the Exception when firing events/jobs from the Controller in a post method?

All answers will we welcomed,

Thank you in advanced

0 likes
6 replies
christopher's avatar

Without the old HTML Package you have to include the csrf Token in your Form manually.

<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">

-> http://laravel.com/docs/master/routing#csrf-protection

The Middleware is protecting your Requests. For example: You use also Middleware to make a Role Based Management System. -> http://laravel.com/docs/master/middleware

For example if you`re building an APP with Angular. CSRF is protecting you.

Take a bit and read also the awesome docs.

1 like
Polinicles's avatar

Thank you for your answer!

I'm currently using the illuminate/html package so then I don't have to add it, do I?

In the official documentation says: "You do not need to manually verify the CSRF token on POST, PUT, or DELETE requests."

I guess that the only thing I have to put it's the csrf_token() in the meta-tag of the header. Don't I?

Do you have any idea of why I had so many errors just because I used the Events/Jobs in the Controller's POST method? I removed them from my method and stop having the TokenMismatchException in the VerifyCsrfToken.php.

ricardovigatti's avatar

i noticed but, if I got here, it is likely that others will also arrive. xD

Please or to participate in this conversation.