0

I'm kinda new on Laravel. I have created some routes for GET and POST, but the only working fine is the GET ones. The POST ones always returns TokenMismatchException, even though the process inside and the database query is executed properly. Some of the questions I saw on SO usually talking about AJAX, and the solution involves sending token. But I don't use any AJAX... yet, and usually the answers said that if I use an ordinary POST form, Laravel will insert a hidden token.

Here's the routes code:

Route::post('/practice/{level_id?}/{group_id?}/{command?}', 'WebController@practice');
Route::get('/practice/{level_id?}/{group_id?}/{command?}', 'WebController@practice');

And here's how I receive it on the webcontroller:

public function practice($level_id = "", $group_id = "", $command = "")

Is there something wrong? What could possibly makes my POST request fail? Thanks.

Chen Li Yong
  • 5,459
  • 8
  • 58
  • 124

2 Answers2

1

Please add a hidden field in your form.If you are using Laravel form builder then its automatically adds a hidden token field to your form when you do Form::open().
Try with Something like this:

<input name="_token" type="hidden"  value="{{ csrf_token() }}">
Chonchol Mahmud
  • 2,717
  • 7
  • 39
  • 72
1

If you are using Form::open method which doesn't come with new versions of Laravel you have to install it from here, then there will be no problem of TokenMismatchException. But if you are using HTML5 Form tags then you need to add {{ csrf_field() }} in your HTML Form which will generate

<input type="hidden" name="_token" value="53sfsffxth7AYe4RFSjzaPf2ygLCecJhbkhblah">
Iftikhar uddin
  • 3,117
  • 3
  • 29
  • 48