Generate Security Token

How you can generate Csrf token in laravel

we are generating csrf token for the form.

Create a controller

//write command in terminal

PHP artisan make:controller ControllerName

now go in http/controllers and write into your controller my controller name is 'my_controller'.

class my_controller extends Controller
{
    //
    public function viewfile(){
        return view('form');//it will show form
    }
    public function data_file(Request $request)
    {
        echo "<pre>";
        print_r($request->all()); //it will show token request
    }
}

View

Now we will make a view with form and csrf token.

//('/data') mentioned in action is uri in controller so the token we will get will be on /data
<form action="{{url('/data')}}" method="post"> 
 @csrf
  <div class="form-group">
    <label for="exampleInputEmail1">Email address</label>
    <input type="email" name="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
    <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Password</label>
    <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password" name="pass">
  </div>
  <div class="form-group form-check">
    <input type="checkbox" class="form-check-input" id="exampleCheck1">
    <label class="form-check-label" for="exampleCheck1">Check me out</label>
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>
</form>

Routing

We will go in web.php file and then


//we will add path of my_controller to access functions
use App\Http\Controllers\my_controller;

Route::view('/clo','hide');

Route::get('/form',[my_controller::class,'viewfile']);//we will get values

Route::post('/data',[my_controller::class,'data_file']);//and post here

Output

When values will be entered in the form than you'll be redirected towards /data to show token