代码如下
Route::group(['prefix'=>'admin','namespace'=>'Admin'],function (){
//登录
Route::match(['get','post'],'/','Index@login');
//注册
Route::match(['get','post'],'register','Index@register');
});
form表单代码如下
<form>
<div class="loginbox-title">注册div>
<div class="loginbox-or">
<div class="or-line">div>
div>
<div class="loginbox-textbox">
<input type="text" class="form-control" name="username" placeholder="用户名" />
div>
<div class="loginbox-textbox">
<input type="password" class="form-control" name="password" placeholder="密码" />
div>
<div class="loginbox-textbox">
<input type="password" class="form-control" name="conpass" placeholder="确认密码" />
div>
<div class="loginbox-textbox">
<input type="text" class="form-control" name="nickname" placeholder="昵称" />
div>
<div class="loginbox-textbox">
<input type="email" class="form-control" name="email" placeholder="邮箱" />
div>
<div class="loginbox-submit">
<input type="submit" id="register" class="btn btn-primary btn-block" value="注册">
div>
<div class="loginbox-signup">
<a href="{{url('admin')}}">返回登录a>
div>
form>
代码如下
$(function () {
$('#register').click(function () {
$.ajax({
url:"{{url('admin/register')}}",
data:$('form').serialize(),
type:'post',
dataType:"json",
success:function (data) {
if(data.code==1){
layer.msg(data.msg,{
icon:6,
time:2000,
},function () {
location.href=data.url;
});
}else {
layer.open({
title:"注册失败",
content:data.msg,
icon:5,
anim:6
});
}
}
});
return false;
});
});
代码如下
//注册
public function register()
{
if (request()->isMethod('post')){
$data=request()->only(['username','password','conpass','nickname','email']);
$result=(new Admin())->register($data);
if ($result==1){
$msg=[
'code'=>1,
'msg'=>"用户注册成功",
'url'=>url('admin')
];
}else{
$msg=[
'code'=>0,
'msg'=>$result
];
}
return $msg;
}
return view('admin.index.register');
}
代码如下:
//注册
public function register($data){
$rule=[
'username'=>'bail|required|unique:admins',
'password'=>'required',
'conpass'=>'required|same:password',
'nickname'=>'required',
'email'=>'required|email|unique:admins'
];
$msg=[
'username.required'=>'用户名不能为空',
'username.unique'=>'用户名已存在',
'password.required'=>'密码不能为空',
'conpass.required'=>'确认密码不能为空',
'conpass.same'=>'两次输入密码不一致',
'nickname.required'=>'昵称不能为空',
'email.required'=>'邮箱不能为空',
'email.email'=>'邮箱格式不正确',
'email.unique'=>'此邮箱已经注册过'
];
$validator=Validator::make($data,$rule,$msg);
if ($validator->fails()){
return $validator->errors()->first();
}
$result=$this->create($data);
if ($result){
return 1;
}else{
return "用户注册失败";
}
}