laravel 使用自带的用户注册登录功能并修改登录方式

1,首先配置好数据库

修改.env 文件

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=feng_app
DB_USERNAME=root
DB_PASSWORD=123456

2,执行迁移

php artisan migrate

3,生成路由,控制器,视图文件

php artisan make:auth

4,测试是否生成成功

http://your-site/login

5,修改登录方式,使用name字段登录,而不是email。

方式1,修改文件Illuminate\Foundation\Auth\AuthenticatesUsers

vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers



public function username()
  {
    // return 'email';
    retrun  'name';
  }

修改视图文件resources/views/auth/login.blade.php

  <form class="form-horizontal" method="POST" action="{{ route('login') }}">
                        {{ csrf_field() }}

                        <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                            <label for="name" class="col-md-4 control-label">name</label>

                            <div class="col-md-6">
                                <input id="name" type="name" class="form-control" name="name" value="{{ old('name') }}" required autofocus>

                                @if ($errors->has('name'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('name') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                            <label for="password" class="col-md-4 control-label">Password</label>

                            <div class="col-md-6">
                                <input id="password" type="password" class="form-control" name="password" required>

                                @if ($errors->has('password'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('password') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me
                                    </label>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-8 col-md-offset-4">
                                <button type="submit" class="btn btn-primary">
                                    Login
                                </button>

                                <a class="btn btn-link" href="{{ route('password.request') }}">
                                    Forgot Your Password?
                                </a>
                            </div>
                        </div>
                    </form>

  

方式2

修改控制器文件,重写username() 方法

public function username()
    {
        return 'name';
    }

 

6,修改注册控制器


namespace App\Http\Controllers\Auth;



use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\DB;

.......


protected
function validator(array $data) { $mobile=$data['name']; $sms_code=DB::select("select sms_code from sms where phone=? order by id desc limit 1",[$mobile]); return Validator::make($data, [ 'name' => 'required|string|max:255', // 'email' => 'required|string|email|max:255|unique:users', 'password' => 'required|string|min:6|confirmed', 'tcode' => 'required|string|max:25', 'code' => [ 'required', 'string', 'max:25', function($attribute, $value, $fail)use($sms_code) { $sms=intval($sms_code[0]->sms_code); if ($value!= $sms) { return $fail($value.'验证码错误'.$sms); } }, ], ]); } /** * Create a new user instance after a valid registration. * * @param array $data * @return \App\User */ protected function create(array $data) { return User::create([ 'name' => $data['name'], //'email' => $data['email'], 'password' => bcrypt($data['password']), 'tcode' => $data['tcode'] ]); }

 修改视图文件

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">用户注册</div>

                <div class="panel-body">
                    <form class="form-horizontal" method="POST" action="{{ route('register') }}">
                        {{ csrf_field() }}

                        <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                            <label for="name" class="col-md-4 control-label">手机号</label>

                            <div class="col-md-6">
                                <input id="name" type="text" class="form-control" name="name"  value="{{ old('tcode') }}" required autofocus>

                                @if ($errors->has('name'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('name') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
 
                        <div class="form-group">
                            <label class="col-md-4 control-label">验证码</label>

                            <div class="col-md-4">
                                <input id="code" type="text" class="form-control" name="code"  value="{{ old('code') }}" required> 
                                
                                 @if ($errors->has('code'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('code') }}</strong>
                                    </span>
                                @endif
                            </div>
                             <div class="col-md-2">
                                <button type="button" class="btn btn-primary" id="GetVerify" onclick="get_verify()">
                                    发送验证码
                                </button>
                                 
                            </div>
                        </div>
 
                        <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                            <label for="password" class="col-md-4 control-label">密码</label>

                            <div class="col-md-6">
                                <input id="password" type="password" class="form-control" name="password" required>

                                @if ($errors->has('password'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('password') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="password-confirm" class="col-md-4 control-label">再次输入密码</label>

                            <div class="col-md-6">
                                <input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
                            </div>
                        </div>
                        
                        <div class="form-group{{ $errors->has('tcode') ? ' has-error' : '' }}">
                            <label class="col-md-4 control-label">推荐码</label>

                            <div class="col-md-4">
                                <input id="tcode" type="text" class="form-control" name="tcode"  value="{{ old('tcode') }}" required> 
                                  <span class="help-block">
                                        <strong></strong>
                                    </span>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <button type="submit" class="btn btn-primary">
                                    Register
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
             <!-- 
                var times = 120;
                var isinerval;
                function get_verify() {
                     
                    var mobile = $("#name").val();
                    var partten = /^1[3-9]\d{9}$/;
                    if(!partten.test(mobile)){
                        alert("请输入正确的手机号码");
                        $('#name').focus();
                        return false;
                    }
                    
                    $.get("/sms",{ mobile: mobile }, function(data){
                        if(data=="0") {
                            $("#mobile_send").html(mobile);
                            $("#mobile_div").css("display","none");
                            $("#mobile_send_div").css("display","");
                             
                            times = 120;
                            $("#GetVerify").css("display","");
                            $("#GetVerify").attr("disabled", true);
                            isinerval = setInterval("CountDown()", 1000);
                        } else if(data=="-1") {
                            alert("你今天获取验证码次数已达到上限");
                        } else if(data=="-100") {
                            $('#code').val('');
                            alert("验证码已失效,请点击图片验证码获取新的验证码!");
                            $('#code').focus();
                        } else if(data=="-101") {
                            alert("验证码错误!");
                            $('#code').focus();
                        } else {
                            alert("短信发送失败");
                        }
                    });
                    
                }
            function CountDown() {
                    if (times < 1) {
                        $("#GetVerify").html("获取短信验证码").attr("disabled", false);
                        $("#edit_mobile").css("display","");
                        clearInterval(isinerval);
                        return;
                    }
                    $("#GetVerify").html(times+"秒后重获");
                    times--;
                }
                function edit_mobile() {
                    $("#mobile_div").css("display","");
                    $("#mobile_send_div").css("display","none");
                }
            //-->
</script>

 

posted @ 2021-12-04 16:16  fsl  阅读(834)  评论(0编辑  收藏  举报