要求:当地址栏中出现“.../public/学号/login”时,显示登录页面(使用提供的登录页面login.blade.php)。单击登录按钮后,可以通过控制器NewsController中的loginServer方法对信息验证后登录。使用Auth对用户名和密码进行验证,登录成功后,跳转到list页面。登录失败后,跳转到登录页面。
时间: 2024-03-27 07:37:30 浏览: 16
好的,你的问题是如何实现地址栏中出现“.../public/学号/login”时,显示登录页面并验证登录信息?
首先,你需要在路由中设置一个路由来处理这个请求,例如:
```
Route::get('/{student_id}/login', 'LoginController@index');
```
这个路由会将请求转发到LoginController的index方法,然后在这个方法中,你需要返回登录页面login.blade.php,例如:
```
public function index($student_id)
{
return view('login');
}
```
接下来,你需要在登录页面中添加一个表单,让用户输入用户名和密码,并提交到NewsController的loginServer方法中进行验证,例如:
```
<form action="/news/login" method="POST">
@csrf
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="Login">
</form>
```
在NewsController中,你需要实现loginServer方法来验证用户的登录信息,并使用Auth进行验证,例如:
```
public function loginServer(Request $request)
{
$credentials = $request->only('username', 'password');
if (Auth::attempt($credentials)) {
return redirect()->route('news.list');
}
return redirect('/{student_id}/login')->withErrors('Invalid login credentials');
}
```
如果验证成功,就会重定向到list页面,如果验证失败,就会重定向到登录页面,并显示错误信息。记得在list页面中使用Auth中间件来保护页面,只有登录用户才能访问该页面,例如:
```
Route::middleware(['auth'])->group(function () {
Route::get('/{student_id}/list', 'NewsController@list')->name('news.list');
});
```
这样就完成了登录验证的过程。