laravel 7 登录

1:路由,展示登录表单

Route::group(['prefix'=>'day','namespace'=>'day18'],function (){
//    登录
    Route::get('login','ExamController@login');

});

2,HTML:

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <link href="/static/static/h-ui/css/H-ui.min.css" rel="stylesheet" type="text/css" />
    <link href="/static/static/h-ui.admin/css/H-ui.login.css" rel="stylesheet" type="text/css" />
    <link href="/static/static/h-ui.admin/css/style.css" rel="stylesheet" type="text/css" />
    <link href="/static/lib/Hui-iconfont/1.0.8/iconfont.css" rel="stylesheet" type="text/css" />
    <title>后台登录 - H-ui.admin v3.1</title>
    <link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">
</head>
<body>
<input type="hidden" id="TenantId" name="TenantId" value="" />
<div class="header"></div>
<div class="loginWraper">
    <div id="loginform" class="loginBox">
        <form class="form form-horizontal" action="{{url('day/dologin')}}" method="post">
           @csrf
            <div class="row cl">
                <label class="form-label col-xs-3"><i class="Hui-iconfont">&#xe60d;</i></label>
                <div class="formControls col-xs-8">
                    <input id="" name="username" type="text" placeholder="账户" class="input-text size-L">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-3"><i class="Hui-iconfont">&#xe60e;</i></label>
                <div class="formControls col-xs-8">
                    <input id="" name="password" type="password" placeholder="密码" class="input-text size-L">
                </div>
            </div>


            <div class="row cl">
                <div class="formControls col-xs-8 col-xs-offset-3">
                    <input name="" type="submit" class="btn btn-success radius size-L" value="&nbsp;登&nbsp;&nbsp;&nbsp;&nbsp;录&nbsp;">
                    <input name="" type="reset" class="btn btn-default radius size-L" value="&nbsp;取&nbsp;&nbsp;&nbsp;&nbsp;消&nbsp;">
                </div>
            </div>
        </form>
{{--        显示错误信息--}}
        @if (count($errors) > 0)
            <div class="alert alert-danger">
                <ul>
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif
    </div>
</div>
<div class="footer">Copyright 你的公司名称 by H-ui.admin v3.1</div>
<script type="text/javascript" src="/static/lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="/static/static/h-ui/js/H-ui.min.js"></script>
<!--此乃百度统计代码,请自行删除-->
<script>
    var _hmt = _hmt || [];
    (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?080836300300be57b7f34f4b3e97d911";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
</script>

</body>
</html>

3:提交表单路由:

Route::group(['prefix'=>'day','namespace'=>'day18'],function (){

//    处理登录
    Route::post('dologin','ExamController@doLogin');
});

4:提交控制器验证


public function doLogin(Request $request){
// 去除token
$params=$request->except('_token');
// 验证非空
$this->validate($request, [
'username' => 'required',
'password' => 'required'
], [
'username.required' => '账号不可以为空',
'password.required' => '密码不可以为空'
], $params);
// 匹配数据库
$res=Admins::login($params);
// 验证账号和密码
if ($res['username']!==$params['username']){
return redirect(url('day/login'))->withErrors(['error'=>'账号错误']);
}elseif ($res['password']!==md5($params['password'])){
return redirect(url('day/login'))->withErrors(['error'=>'密码错误']);
}else{
// 记录session通过put方法
$request->session()->put('username', $res['username']);
//通过全局辅助函数
// session(['username' => $res['username']);
//验证成功跳转首页
return redirect(url('day/new'))->with('success','登录成功');
}

}

5:模型代码:

<?php

namespace App\models;

use Illuminate\Database\Eloquent\Model;

class Admins extends Model
{
    //链接表名
    protected $table = 'admins';
//验证数据
    public static function login($params)
    {
//验证账号
return self::where('username',$params['username']) ->first(); } }

6:效果:

 

posted @ 2021-11-18 17:30  王越666  阅读(86)  评论(0编辑  收藏  举报