初学者尝试使用laravel将基本表单提交到数据库中。我知道我应该使用' get'显示注册表形式,但是当我这样做时,我得到了' methodnotallowedhttpexception错误。
目前,当我在表单中输入数据并按提交时,只刷新页面。非常感谢任何帮助,谢谢
web.php
Route::post('registerForm', 'AuthController@viewregisterForm');
Route::post('registerUser', 'AuthController@registerUser');
AuthController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
//use Illuminate\Support\Facades\Auth;
//use App\Http\Requests;
use App\User;
class AuthController extends Controller
{
function viewregisterForm()
{
return view('register/registerForm');
}
function registerUser(Request $request)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required',
'password' => 'required',
'dateofbirth' =>'required',
]);
//create a Film object
$user = new $User();
$user->name = $request->name;
$user->email = $request->email;
$user->password = $request->password;
$user->dateofbirth = $request->dateofbirth;
$user->role = 1;
$user->save();
return redirect('all');
}
}
?>
registerForm.blade
@extends('layouts.master')
@section('title', 'Register user')
@section('content')
<form action="{{url('registerForm')}}" method="POST">
{{ csrf_field() }}
<h1>Register user</h1>
<div>
<label for="title">Enter name</label>
<input type="text" name="name" id="name">
</div>
<div>
`enter code here`<label for="title">Enter email</label>
<input type="text" name="email" id="email">
</div>
<div>
<label for="title">Enter password</label>
<input type="text" name="password" id="password">
<label for="title">Enter date of birth</label>
<input type="text" name="dateofbirth" id="dateofbirth">
</div>
<input type="submit" name="submitBtn" value="Add User">
</form>
@endsection
答案 0 :(得分:1)
尝试将路线更改为:
Route::get('registerForm', 'AuthController@viewregisterForm');
Route::post('registerForm', 'AuthController@registerUser');
答案 1 :(得分:1)
您需要更改表单提交的操作
这
<form action="{{ url('registerForm') }}" method="POST">
到
<form action="{{ route('register-user') }}" method="POST">
并从
更改路线Route::post('registerUser', 'AuthController@registerUser');
到
Route::post('registerUser', ['as' => 'register-user', 'uses' => 'AuthController@registerUser']);