简体   繁体   中英

Laravel: can't redirect to the correct page after submitting form

So I have an issue where i can't seem to get it to redirect to the dashboard after a user signs up by submitting their details to the database. Instead i get this message in firefox: "The page has expired due to inactivity.Please refresh and try again." and in the url it does not say /dashboard it says /signup.

This is my web.php route

Route::get('/', function () {
    return view('user/usersignup');
});


Route::post('/signup', [
    'uses' => 'UserController@postSignUp',
    'as' => 'signup'
]);

Route::post('/signin', [
    'uses' => 'UserController@postSignIn',
    'as' => 'signin'
]);

Route::get('/dashboard', [
    'uses' => 'UserController@getDashboard',
    'as' => 'dashboard'
]);

This is UserController, my controller.

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

    class UserController extends Controller
    {
       public function getDashboard(){
           return view('dashboard');
       }

       public function postSignUp(Request $request){
           $first_name = $request['first_name'];
           $email = $request['email'];
           $password = bcrypt($request['password']);

           $user= new User();
           $user->first_name = $first_name;
           $user->email = $email;
           $user->password = $password;

           $user->save();
           return redirect()->route('dashboard');
       }
    }

This is my signup view: usersignup.blade.php

  <form action="{{ route('signup') }}" method="post" id="userform">

     <div class="form-group">
        <label for="first_name">Your first Name</label>
        <input class="form-control" type="text" name="first_name" id="fullname">
     </div>

     <div class="form-group">
         <label for="email">Your E-Mail</label>
         <input class="form-control" type="text" name="email" id="email">
     </div>

     <div class="form-group">
         <label for="password">Your Password</label>
         <input class="form-control" type="password" name="password" id="password">
     </div>
     <button type="submit" class="btn btn-primary">Submit</button>
     <input type="hidden" name="_token" value="{{ Session::token() }}">
 </form>

You are using Wrong CSRF .

Either use {{ csrf_field() }} inside <form> tag and remove hidden element name="_token"

<form action="{{ route('signup') }}" method="post" id="userform">
{{ csrf_field() }}
.....
.....
<button type="submit" class="btn btn-primary">Submit</button>
</form>

Or replace value of hidden element _token to csrf_token()

<form action="{{ route('signup') }}" method="post" id="userform">
.....
.....
<button type="submit" class="btn btn-primary">Submit</button>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

Add a csrf field to the form instead of hidden _token input:

<form action="{{ route('signup') }}" method="post" id="userform">
     {{ csrf_field() }}

     <div class="form-group">
        <label for="fullname">Your Full Name</label>
        <input class="form-control" type="text" name="fullname" id="fullname">
     </div>

     <div class="form-group">
         <label for="email">Your E-Mail</label>
         <input class="form-control" type="text" name="email" id="email">
     </div>

     <div class="form-group">
         <label for="password">Your Password</label>
         <input class="form-control" type="password" name="password" id="password">
     </div>
     <button type="submit" class="btn btn-primary">Submit</button>
 </form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM