Laravel: 提交表单后无法重定向到正确的页面

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

所以我有一个问题,在用户通过将他们的详细信息提交到数据库进行注册后,我似乎无法让它重定向到仪表板。相反,我在 firefox 中收到此消息:"The page has expired due to inactivity.Please refresh and try again." 并且在 url 中它没有说 /dashboard 而是说 /signup.

这是我的web.php路线

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'
]);

这是 UserController,我的控制器。

<?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');
       }
    }

这是我的注册视图: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>

将 csrf 字段添加到表单而不是隐藏的 _token 输入:

<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>

你用错了CSRF

<form> 标签内使用 {{ csrf_field() }} 并删除隐藏元素 name="_token"

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

或者将隐藏元素_token的值替换为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>