Laravel路由未定义异常

时间:2019-01-27 17:26:32

标签: php laravel laravel-5 laravel-blade

我要发布数据以插入用户。以下是HTML中的代码段,该代码段将用于添加用户。

<form id="register-form" action="{{ route('register.add') }}" method="post" role="form" style="display: none;">
    <div class="form-group">
        <input type="text" name="name" id="name" tabindex="1" class="form-control" placeholder="Username" value="">
    </div>
    <div class="form-group">
        <input type="email" name="email" id="email" tabindex="1" class="form-control" placeholder="Email Address" value="">
    </div>
    <div class="form-group">
        <input type="password" name="password" id="password" tabindex="2" class="form-control" placeholder="Password">
    </div>
    <div class="form-group">
        <input type="password" name="confirm-password" id="confirm-password" tabindex="2" class="form-control" placeholder="Confirm Password">
    </div>
    <div class="form-group">
        <div class="row">
            <div class="col-sm-6 col-sm-offset-3">
                <input type="submit" name="register-submit" id="register-submit" tabindex="4" class="form-control btn btn-register" value="Register Now">
            </div>
        </div>
    </div>
</form>

路线

Route::resource('register', 'PassportController');

ProductController

public function add(Request $request)
{
    $this->validate($request, [
        'name' => 'required|min:3',
        'email' => 'required|email|unique:users',
        'password' => 'required|min:6',
    ]);

    $user = User::create([
        'name' => $request->name,
        'email' => $request->email,
        'password' => bcrypt($request->password)
    ]);

    $token = $user->createToken('TutsForWeb')->accessToken;

    return response()->json(['token' => $token], 200);
}

运行服务器时,它将引发异常。

  

未定义路由[register.add]。 (视图:   \ apiauthexample \ resources \ views \ welcome.blade.php)

1 个答案:

答案 0 :(得分:0)

那是因为Laravel的资源控制器没有add()动作。 Route::resource方法寄存器indexcreatestoreshoweditupdatedestroy路由。您可以在official documentation中详细了解它。

根据您的情况判断,您需要将add方法重命名为store,并通过route('register.store')引用此路由。