效果
web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
use App\Http\Controllers\PhotoController;
use App\Http\Controllers\ProvisionServer;
use App\Http\Controllers\SUserController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
//根路由
Route::get('/', function () {
return view('welcome');
});
Route::get('/login',[SUserController::class,'login']);
Route::post('/loginCheck',[SUserController::class,'loginCheck']);
Route::resource('/su',SUserController::class);
login
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>用户登录</title>
</head>
<body>
<div class="login">
<form action="{{url('loginCheck')}}" method="post">
<h2>用户登录</h2>
<hr/><hr/>
<div>
<label for="username">用户名:</label>
<input id="username" type="text" name="username" placeholder="请输入用户名" required>
</div><hr/>
<div>
<label for="password">密码:</label>
<input id="password" type="password" name="password" placeholder="请输入密码" required>
</div><hr/>
<div>
<input type="submit" value="登录">
<input type="reset">
</div>
@csrf
</form>
</div>
<style>
body{
background-color: lightpink;
}
body *{
box-sizing: border-box;
}
.login form{
display: flex;
flex-direction: column;
height: 300px;
width: 600px;
background-color: mistyrose;
margin: 50px auto;
padding:10px 40px;
box-shadow: 0px 3px 6px #888;
}
.login h2{
text-align: center;
color: seagreen;
}
form div{
margin: 0 auto;
}
.login *{
margin-bottom: 10px;
}
form div:last-of-type{
/* margin-left: 80px; */
}
form div:last-of-type input{
margin-left: 40px;
}
.login label{
display: inline-block;
width: 80px;
text-align: right;
}
</style>
@if(!empty($data['message']))
<script>alert("{{$data['message']}}")</script>
@endif
</body>
</html>
Sucontroller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class SUserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
$res=DB::table('users')->get();
dd($res);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function login(){
return view('login');
}
public function loginCheck(Request $request){
$data=$request->all();
$username=$request->input('username');
$password=$request->input('password');
//dd($data,$username);
$res=DB::table('users')
->where('name',$username)
->where('password',$password)
->first();
// dd($res);
if($res){
echo"登录成功";
}
else{
echo "登录失败";
}
}
}