英文:
Login Authentication in Laravel 10
问题
以下是翻译好的部分:
错误信息:
“POST”方法不支持登录路由。支持的方法有:GET、HEAD。
英文:
I am trying login authentication but showing an error message. Hope an expert helps me to get the solution.
This is my HTML code:
<form action="{{url('/')}}/login" method="post">
@csrf
<div class="fields">
<div class="input-field">
<label for="un" >UserName</label>
<div class="inpt">
<input type="text" id="un" placeholder="Enter UserName" required></div>
</dev>
</div>
<div class="input-field">
<label for="pw">Password</label>
<div class="inpt">
<input type="password" id="pw" placeholder="Enter Your password" required>
</div>
</div>
<div>
<input type="checkbox" id="rm" >
<label for="rm">Remember Me</label>
</div>
<div class="input-field-1">
<button type="submit">Login</button>
</div>
</div>
</form>
This is my controller:
public function login():View
{
return view('login');
}
public function authenticate(Request $request)
{
$credentials = $request->validate([
'email' => 'required|email',
'password' => 'required'
]);
if(Auth::attempt($credentials))
{
$request->session()->regenerate();
return redirect()->route('index')
->withSuccess('You have successfully logged in!');
}
return back()->withErrors([
'email' => 'Your provided credentials do not match our records.',
])->onlyInput('email');
}
This is my route(web.php):
Route::get('/login',[Registrationcontroller::class,'login'])->name('login');
Route::post('/authenticate', [Registrationcontroller::class, 'authenticate'])->name('authenticate');
Error:
The POST method is not supported for route login. Supported methods: GET, HEAD.
答案1
得分: 1
你应该调整表单的 "action" 属性,以将表单提交到你的 "authenticate" 路由:
<form action="{{ route('authenticate') }}" method="post">
使用 route
方法会为我们提供一个命名路由的绝对 URL。
英文:
You should adjust your form's "action" attribute to submit the form to your "authenticate" route:
<form action="{{ route('authenticate') }}" method="post">
Using the route
method gives us an absolute URL to a named route.
答案2
得分: 0
警告:您的/login
路由只接受GET方法,正如您在web.php中所看到的。这是好的,因为在您的登录路由和控制器函数中,您将其用于显示带有登录表单的视图。
您应该发出POST请求到/authenticate
路由,其中将验证您的凭据,如果正确,将重定向您到带有经过身份验证的会话的索引页面,否则将将您发送回登录视图。
您还应该在您的表单操作中使用{{ route('authenticate') }}
函数,以便Laravel以正确的方式生成URL。
话虽如此,您还应该在您的表单上显示错误,这样如果身份验证出现问题,您就会知道!
希望这有所帮助。
英文:
Beware: your /login
route accepts GET method only, as you can see in your web.php. And that is good because in you login route and controller function u use it for show the view with the login form.
You should make a POST request to /authenticate
route where your credentials would be validated and, if correct it would redirect you to the index with an authenticated session, otherwise would send you back to the login view.
You should also use the {{ route('authenticate') }}
function in your form action to make laravel generate the URL the right way.
That being said you should also show the errors on your form, so if there is something going wrong with authentication you would know!
Hope this helps
答案3
得分: 0
您的表单操作应指向/authenticate
端点,您还可以使用命名路由{{ route('authenticate') }}
。
您的HTML表单代码应如下:
<form action="{{url('/')}}/authenticate" method="post">
@csrf
<div class="fields">
<div class="input-field">
<label for="un">用户名</label>
<div class="inpt">
<input type="text" id="un" placeholder="输入用户名" required>
</div>
</div>
<div class="input-field">
<label for="pw">密码</label>
<div class="inpt">
<input type="password" id="pw" placeholder="输入您的密码" required>
</div>
</div>
<div>
<input type="checkbox" id="rm">
<label for="rm">记住我</label>
</div>
<div class="input-field-1">
<button type="submit">登录</button>
</div>
</div>
</form>
或者:
<form action="{{ route('authenticate') }}" method="post">
@csrf
<div class="fields">
<div class="input-field">
<label for="un">用户名</label>
<div class="inpt">
<input type="text" id="un" placeholder="输入用户名" required>
</div>
</div>
<div class="input-field">
<label for="pw">密码</label>
<div class="inpt">
<input type="password" id="pw" placeholder="输入您的密码" required>
</div>
</div>
<div>
<input type="checkbox" id="rm">
<label for="rm">记住我</label>
</div>
<div class="input-field-1">
<button type="submit">登录</button>
</div>
</div>
</form>
当您使用命名路由时,如果有任何前缀名称,请小心。
英文:
Your form action should be pointed to the /authenticate
endpoint, also you can use named route {{ route('authenticate') }}
Your HTML form code should be
<form action="{{url('/')}}/authenticate" method="post">
@csrf
<div class="fields">
<div class="input-field">
<label for="un" >UserName</label>
<div class="inpt">
<input type="text" id="un" placeholder="Enter UserName" required></div>
</dev>
</div>
<div class="input-field">
<label for="pw">Password</label>
<div class="inpt">
<input type="password" id="pw" placeholder="Enter Your password" required>
</div>
</div>
<div>
<input type="checkbox" id="rm" >
<label for="rm">Remember Me</label>
</div>
<div class="input-field-1">
<button type="submit">Login</button>
</div>
</div>
</form>
OR:
<form action="{{ route('authenticate') }}" method="post">
@csrf
<div class="fields">
<div class="input-field">
<label for="un" >UserName</label>
<div class="inpt">
<input type="text" id="un" placeholder="Enter UserName" required></div>
</dev>
</div>
<div class="input-field">
<label for="pw">Password</label>
<div class="inpt">
<input type="password" id="pw" placeholder="Enter Your password" required>
</div>
</div>
<div>
<input type="checkbox" id="rm" >
<label for="rm">Remember Me</label>
</div>
<div class="input-field-1">
<button type="submit">Login</button>
</div>
</div>
</form>
When you use a named route you should be careful if you have any prefix name
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论