Urarii
Urarii

Reputation: 67

laravel 5 registration form

I am trying to make a registration form in laravel 5 and it gives me this error:

Whoops, looks like something went wrong.

1/1 TokenMismatchException in compiled.php line 2440:

This is my views/register.blade.php:

<form class="form-horizontal" action = "inregistrare_process" method = "POST">
.....
</form>

This is in my app/Register.php

    <?php namespace App;

use Illuminate\Database\Eloquent\Model;


class Register extends Eloquent {
        protected $guarded = array();
        protected $table = 'users'; 
        public $timestamps = 'false' ; 


        public static function saveFormData($data)
        {
            DB::table('users')->insert($data);
        }

}

This is my Controllers/RegisterController.php

<?php namespace App\Http\Controllers;
use App\Register;
class RegisterController extends Controller {



public function store()
{
    Register::saveFormData(Input::except(array('_token')));
}

}

And this is in my routes.php

Route::get('inregistrare',function(){

    return view('register');
});

Route::post('inregistrare_process', function()
{
        $obj = new RegisterController() ;
        return $obj->store();
});

Can someone help me make this work or give me another alternative on how to make the registration page work ?

Upvotes: 1

Views: 653

Answers (1)

Nehal Hasnayeen
Nehal Hasnayeen

Reputation: 1223

  1. First of all you need a hidden input type with csrf_token in form

insert below line after your form declaration

<input type="hidden" name="_token" value="{{ csrf_token() }}">
  1. Do you have write permission for your session file. By default session is stored in /storage/framework/sessions/ check that you have write permission on that directory.

  2. If you are using an Ajax request than add the tokenMatch() method to app/Http/Middleware/VerifyCsrfToken.php.

    protected function tokensMatch($request)
    {
        $token = $request->ajax() ? $request->header('X-CSRF-Token') : $request->input('_token');
    
        return $request->session()->token() == $token;
    }
    

and add this in your js file

$.ajaxSetup(
{
    headers:
    {
        'X-CSRF-Token': $('input[name="_token"]').val()
    }
});

Upvotes: 2

Related Questions