laravel5.3 Register Log in - Register

php-laravel framework for registration

To use laravel framework, I came up with the basic elements of laravel 3 framework.
  1. In the route directory defined routing, define the route the way: get, post, any, resource, match Xiao Bian here a simple look.
  2. According to just create a good route, create the controller Controller, create recommended to use php artisan, write the corresponding business logic controllers inside.
  3. The need to create a view file, index.blade.php
1, registered route

a, Xiao Bian here to register routing using laravel middleware, middleware do not know if there is a small partner, you can go laravel Chinese network laravel Chinese network to see.

使用路由组定义路由,方便操作
Route::group(['namespace'=>'Admin','prefix'=>'admins'],function (){
    //注册路由
    Route::match(['get','post'],'register','RegisterController@register');
});    
2, the registration controller

a, using laravel form validation data determination request is reasonable.
b, using the built-in laravel send messages, user activated if the message is not sent, check the small series of this article laravel5.3 send mail
c, using the front-end send ajax request.
4, use this code laravel framework comes, a lot of online tutorials, here is not elaborated. You need to install php artisan. Referring small series codes mounting laravel5.3 codes installed

<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;

class RegisterController extends Controller
{
    //注册路由
    function register(Request $request){
        //var_dump(captcha_img('flat'));die();
        if ($request->isMethod('GET')){
            return view('admin.registerLogin');
        }else{
            //接收数据
            $values = $request->all();
            //验证规则
            $rules = [
              'email'=>'required|unique:user,email|email',
              'user'=>'required|unique:user,user',
              'pwd'=>'required|same:repwd',
              'code'=>'required|captcha',
            ];
            //返回提示信息
            $message = [
              'email.required'=>'邮箱不能为空',
              'email.unique'=>'邮箱已注册,请登录',
              'email'=>'邮箱格式不正确',
              'user.required'=>'用户名不能为空',
              'user.unique'=>'用户名已存在,请重试',
              'pwd.required'=>'密码不能为空',
              'pwd.same'=>'两次密码输入不一致',
              'code.required'=>'验证码不能为空',
              'code.captcha'=>'验证码输入错误'

            ];
            $validator = \Validator::make($values,$rules,$message);
            if ($validator->passes()){
                $user = $request->input('user');
                $email = $request->input('email');
                $result = DB::table('user')->where([['user','=',$user],['email','=',$email]])->first();
                if ($result){
                    $data = [
                      'status'=>'4',
                      'massage'=>'用户已经注册,请重试'
                    ];
                    return $data;
                }else{
                    unset($_POST['repwd']);
                    unset($_POST['code']);
                    $_POST['create_time'] = time();
                    $_POST['pwd'] = md5(md5($_POST['pwd']));
                    $_POST['token'] = str_random(50);
                    $_POST['status'] = 1;
                    $result = DB::table('user')->insertGetID($_POST);
                    if ($result){
                        //发送邮件到注册时的邮箱
                        Mail::send('mail.index',['id'=>$result,'token'=>$_POST['token']],function($message){
                            $message->to($_POST['email']);
                            $message->subject('亲爱的用户,恭喜您注册成功');
                        });
                        //返回邮箱地址,跳转立即激活页面
                        $mail_arr = explode('@',$_POST['email']);
                        $href = "mail.".$mail_arr[1];
                        $data = [
                            'status'=>'200',
                            'href'=>$href,
                            'info'=>'注册成功'
                        ];
                        return $data;
                    }else{
                        $data = [
                            'status'=>'403',
                            'info'=>'注册失败'
                        ];
                        return $data;
                    }
                }
            }else{
                return response()->json([
                    'status'=>'1',
                    'info'=>$validator->errors()->first()
                ]);
            }
        }
    }
3, registration view

a, pop-up prompts the user view layer to use layui;
b, a view I put important documents posted, and other information are registered in the form of the form.
c, js portion should be noted that, Laravel CSRF itself is verified, is enabled by default, since the post request registration, authentication will be carried out CSRF, of course, may be turned off in CSRF in Laravel. Xiao Bian here to write CSRF verification process at the bottom.

<div id="signup-box" class="signup-box widget-box no-border">
<div class="widget-body">
<div class="widget-main">
	<h4 class="header green lighter bigger">
		<i class="ace-icon fa fa-users blue"></i>
		用户注册
	</h4>
	<div class="space-6"></div>
	<p>填写信息: </p>
	<form id="regForm">
		<fieldset>
			<label class="block clearfix">
				<span class="block input-icon input-icon-right">
					<input name="email" type="email" class="form-control" placeholder="邮箱" />
					<i class="ace-icon fa fa-envelope"></i>
				</span>
			</label>
			<label class="block clearfix">
				<span class="block input-icon input-icon-right">
					<input name="user" type="text" class="form-control" placeholder="用户名" />
					<i class="ace-icon fa fa-user"></i>
				</span>
			</label>
			<label class="block clearfix">
				<span class="block input-icon input-icon-right">
					<input name="pwd" type="password" class="form-control" placeholder="密码" />
					<i class="ace-icon fa fa-lock"></i>
				</span>
			</label>

			<label class="block clearfix">
				<span class="block input-icon input-icon-right">
					<input name="repwd" type="password" class="form-control" placeholder="确认密码" />
					<i class="ace-icon fa fa-retweet"></i>
				</span>
			</label>
			<label class="block clearfix">
				<span class="block input-icon input-icon-right">
					<input type="text" class="form-control" name="code" placeholder="输入验证码" />
					<label style="margin-top:30px;">验证码</label>
					<img id="codeReg" style="margin-top:10px;float: right" src="{{ captcha_src('flat') }}" onclick="this.src='/captcha/flat?'+Math.random()" title="点击图片重新获取验证码">
				</span>
			</label>

			<label class="block">
				<input type="checkbox" class="ace" />
				<span class="lbl">
					接受
					<a href="#">用户协议</a>
				</span>
			</label>

			<div class="space-24"></div>

			<div class="clearfix">
				<button type="reset" class="width-30 pull-left btn btn-sm">
					<i class="ace-icon fa fa-refresh"></i>
					<span class="bigger-110">重置</span>
				</button>

				<button id="register" type="button" class="width-65 pull-right btn btn-sm btn-success">
					<span class="bigger-110">注册</span>
					<i class="ace-icon fa fa-arrow-right icon-on-right"></i>
		</button>
</div>	

js part

<script type="text/javascript">
			$.ajaxSetup({
				headers: {
					'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
				}
			});
			//注册
			$("#register").click(function(){
                $("#codeReg").attr('src',src='/captcha/flat?'+Math.random());
				$.ajax({
					type:'post',
					url:'/admins/register',
					dataType:'json',
					data:$("#regForm").serialize(),
					success:function(data){
						if (data.status=='1'){
						    layer.msg(data.info,{icon:5,time:1000});
                        }else if(data.status=='403'){
                            layer.msg(data.info,{icon:5,time:1000});
                        }else{
                            /*layer.confirm('是否立即激活?', {
                                btn: ['确认','取消'] //按钮
                            }, function(){
                                window.location.href="http://www.jb51.net";
                            });*/
                           /* layer.open({
                                type: 1,
                                area: ['300px', '360px'],
                                shadeClose: true, //点击遮罩关闭
                                content: "<a style='width:200px;height: 100px;' href=http://"+data.href+">立即激活</a>"
                            });*/
                           alert('立即激活');
                           window.location.href="http://"+data.href;
                        }
					},
					error:function(){
                        layer.msg("网络错误,稍后重试",{icon:5,time:1000});
                        window.location.reload();
					}
				})
			});
ajax request verification approach to CSRF

a, plus the head in the head meta tags, content write CSRF
B, when used ajaxSetup CSRF ajax request is transmitted to the headers of the request headers inside.

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta id="farm_csrf" name="csrf-token" content="{{ csrf_token() }}">
		<title>登录页面</title>

		<meta name="description" content="User login page" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
		<!-- bootstrap & fontawesome -->
		<link rel="stylesheet" href="/admin/assets/css/bootstrap.min.css" />
		<link rel="stylesheet" href="/admin/assets/font-awesome/4.2.0/css/font-awesome.min.css" />
	</head>
	
//把CSRF的值写进headers里面	
$.ajaxSetup({
	headers: {
		'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
	}
});

Complete results page

Here Insert Picture Description
Here Insert Picture Description

As a programmer, when there are difficulties, but also need some of the chicken soup of the soul, we can focus on my public number. Chicken soup with me.

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/zxh7770/article/details/91476031