关于登录和注册 Laravel自带了一套组件实现了这一功能,我们只需要实现简单的视图即可。

AuthController是专门管理用户注册和登录的。

PassWordController是重置密码用的,今天暂不做记录。


1 配置

我们可以在 config/auth.php 文件中进行用户认证的配置:

<?php

return [

    /*
|--------------------------------------------------------------------------
| Default Authentication Driver
|--------------------------------------------------------------------------
|
| This option controls the authentication driver that will be utilized.
| This driver manages the retrieval and authentication of the users
| attempting to get access to protected areas of your application.
|
| Supported: "database", "eloquent"
|
*/ 'driver' => 'eloquent', /*
|--------------------------------------------------------------------------
| Authentication Model
|--------------------------------------------------------------------------
|
| When using the "Eloquent" authentication driver, we need to know which
| Eloquent model should be used to retrieve your users. Of course, it
| is often just the "User" model but you may use whatever you like.
|
*/ 'model' => App\User::class, /*
|--------------------------------------------------------------------------
| Authentication Table
|--------------------------------------------------------------------------
|
| When using the "Database" authentication driver, we need to know which
| table should be used to retrieve your users. We have chosen a basic
| default value but you may easily change it to any table you like.
|
*/ 'table' => 'users', /*
|--------------------------------------------------------------------------
| Password Reset Settings
|--------------------------------------------------------------------------
|
| Here you may set the options for resetting passwords including the view
| that is your password reset e-mail. You can also set the name of the
| table that maintains all of the reset tokens for your application.
|
| The expire time is the number of minutes that the reset token should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/ 'password' => [
'email' => 'emails.password',
'table' => 'password_resets',
'expire' => 60,
], ];

这是默认的配置,注释写的很清楚了 如果有特别需要可以做更改,一般情况中我们使用默认的就OK。


2 创建路由

/**
* 用户认证
*/
// getLogin 用于展示登录表单。
Route::get('/auth/login', 'Auth\AuthController@getLogin');
// postLogin 用于提交用户登录数据。
Route::post('/auth/login', 'Auth\AuthController@postLogin');
// getLogout 用于退出登录。
Route::get('/auth/logout', 'Auth\AuthController@getLogout'); /**
* 用户注册
*/
// getRegister 用于展示注册表单。
Route::get('/auth/register', 'Auth\AuthController@getRegister');
// postRegister 用于提交用户注册数据。
Route::post('/auth/register', 'Auth\AuthController@postRegister');

3 注册实现

3.1 编写视图

注册视图的路径必须放在 views/auth/ 目录中 并命名为 register.blade.php。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>用户注册</title> <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
</head> <body>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Register</div>
<div class="panel-body">
<form action="{{ url('/auth/register') }}" method="post" role="form" class="form-horizontal">
<input type="hidden" name="_token" value="{{ csrf_token() }}"> <div class="form-group">
<label class="col-md-4 control-label">用户名:</label>
<div class="col-md-6">
<input type="text" name="name" class="form-control" autofocus>
</div>
</div> <div class="form-group">
<label class="col-md-4 control-label">邮箱:</label>
<div class="col-md-6">
<input type="email" name="email" class="form-control">
</div>
</div> <div class="form-group">
<label class="col-md-4 control-label">密码:</label>
<div class="col-md-6">
<input type="password" name="password" class="form-control">
</div>
</div> <div class="form-group">
<label class="col-md-4 control-label">确认密码:</label>
<div class="col-md-6">
<input type="password" name="password_confirmation" class="form-control">
</div>
</div> <div class="form-group">
<div class="col-md-offset-4 col-md-8">
<button type="submit" class="btn btn-primary">注册</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>

3.2 修改跳转URL

注册后跳转的URL有时候不是我们想要的,你可以自定义跳转路由,在AuthController中添加即可:

protected $redirectPath = '/';

4 登录实现

我们注册后已经有了用户了 现在可以试试登录的实现了。

4.1 编写视图

登录的视图路径也是有规定的:views/auth/ 然后命名为:login.balde.php

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>用户登录</title> <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
</head> <body>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Login</div>
<div class="panel-body">
<form action="{{ url('/auth/login') }}" method="post" role="form" class="form-horizontal">
<input type="hidden" name="_token" value="{{ csrf_token() }}"> <div class="form-group">
<label class="col-md-4 control-label">邮箱:</label>
<div class="col-md-6">
<input type="email" name="email" class="form-control">
</div>
</div> <div class="form-group">
<label class="col-md-4 control-label">密码:</label>
<div class="col-md-6">
<input type="password" name="password" class="form-control">
</div>
</div> <div class="form-group">
<div class="col-md-offset-4 col-md-8">
<button type="submit" class="btn btn-primary">登录</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>

4.2 登录后跳转

登录后的跳转跟注册后的跳转是一样的:

protected $redirectPath = '/';

4.3 登录失败跳转

当登录失败了Laravel会默认跳转回 auth/login 路由,这也是可以自定义的:

protected $loginPath = '/error';

4.4 修改登录用户名

默认的登陆用户名是邮箱,我们可以在AuthController中自定义:

// 该属性默认为email,改成name是以用户名作为账号类型登录。
protected $username = 'name';

4.5 查看用户信息

我们可以通过Auth门面的方法来访问已经登录进来的用户:

Auth::user()

4.6 检查用户是否登录

if (Auth::check()) {
// 这个用户已经登录...
}

4.7 用于登录失败次数限制

Laravel支持这种逻辑,我们只需要在AuthController中引入 ThrottlesLogins 这个trait 即可。一分钟内登录5次都不成功就会锁闭一分钟,它是基于 用户名/邮箱和IP地址的。


5 登出用户

我们只需要访问 /auth/logout 就可以登出用户了,当然还有一个方法 就是Auth门面方法:

Auth::logout();

Laravel5.1 登录和注册的更多相关文章

  1. IO流的登录与注册

    import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileR ...

  2. XMPP iOS客户端实现三:登录、注册

    1.创建一个单例模式来管理xmpp的连接和操作 +(XMPPManager *)share { static XMPPManager *_share=nil; static dispatch_once ...

  3. 今天发现之前瑞乐做的登录和注册居然都是用的get请求,瞬间出了一身冷汗.

    今天发现之前瑞乐做的登录和注册居然都是用的get请求,瞬间出了一身冷汗. 然后迅速的让晓勇改成post请求了. 不然我觉得凡是有点抓包能力的人抓到我们登录和注册这么涉及安全的东西居然用的是get请求, ...

  4. 免费 PSD 下载: 20个精美的登录和注册表单

    注册表单有许多不同的形状和尺寸,有的只是单个的输入框,有的则需要多个步骤.登录表单的设计将定义网站的性质,因此它应进行针对性的设计.下面的列表提供了20个醒目的登录和注册表单设计为您提供灵感. 您可能 ...

  5. IOS Storyboard使用-模拟登录、注册、混合使用

    最近分析IOS的占有率,发现5.0以下的少之又少了,故而决定新的App用 Storyboard开发,找了很多资料都是点上的,这个简单的demo是测试代码,发上来,供新手参考. 模拟登录.注册.和显示主 ...

  6. 利用开源项目使discus论坛与java应用同步登录和注册

    最近做了一个资源库系统的项目,老师说可以搭建开源论坛替代自己开发社交模块(评论啊,反馈啊)来减轻负担,甚至提到了要给每个资源开一帖的功能..使我十分怀疑到底是减轻负担还是增加负担...不过怀疑归怀疑, ...

  7. PHP实现登录,注册,密码修改

    注册,登录,修改密码 1.登录 2.忘记密码 3.免费注册 页面布局 <div id="views" class="views"> <div ...

  8. 用纯jsp实现用户的登录、注册与退出

    用户的登录.注册和退出是一个系统最常见的功能,现将各功能用jsp代码表示出来 用户的登录: 其中connDB是数据库连接类,将用户名username放入session中 <%@ page con ...

  9. 使用PHP实现用户登录和注册的功能

    登陆界面 login.php <form action="logincheck.php" method="post"> 用户名:<input ...

随机推荐

  1. git 关联远程分支

    问题解析: git本地新建一个分支后,必须要做远程分支关联.如果没有关联, git 会在下面的操作中提示你显示的添加关联.关联目的是如果在本地分支下操作: git pull, git push ,不需 ...

  2. java.lang.NullPointerException org.apache.jsp.index_jsp._jspInit(index_jsp.java:22)

    java.lang.NullPointerException org.apache.jsp.index_jsp._jspInit(index_jsp.java:22) org.apache.jsp.i ...

  3. [emqttd] (EMQ)

    [emqttd] (EMQ)是采用Erlang语言开发,全面支持MQTT V3.1.1协议,支持集群和大规模连接的开源MQTT消息服务器. [emqttd]致力于发布一个基于Erlang/OTP语言平 ...

  4. Python 射线法判断一个点坐标是否在一个坐标区域内

    class Point: lng = '' lat = '' def __init__(self, lng, lat): self.lng = lng self.lat = lat # 求外包矩形 d ...

  5. 企业信息系统集成框架(设计思路)C++模式

    设计要求: 1.企业信息系统框架.第三方产品通过接口层进行分层. 2.企业信息系统框架如何自由的继承第三方产品:通过一个抽象类.(软件设计要求:模块要求松,接口要求紧). 设计步骤: 1.报文的接受与 ...

  6. SELinux 宽容模式(permissive) 强制模式(enforcing) 关闭(disabled)

    SElinux共有3中状态.1.selinux的配置文件:/etc/selinux/config# This file controls the state of SELinux on the sys ...

  7. 【JAVA设计模式】外观模式(Facade Pattern)

    一  定义 为子系统中的一组接口提供一个一致的界面.Facade模式定义了一个高层的接口,这个接口使得这一子系统更加easy使用. 二  案例 一个子系统中拥有3个模块.每一个模块中都有3个方法.当中 ...

  8. makefile之变量

    1 变量的定义 A variable is a name defined in a makefile to represent a string of text, called the variabl ...

  9. 微信中调起qq

    http://wpa.qq.com/msgrd?uin={$qq}&menu=yes

  10. c# 多线程里面创建byte数组发生内存溢出异常求解

    在多线程里面读取一个400多M的Xml文件,首先将其读入FileStream里面,然后,在执行 byte [] bts = new byte[fs.Length]; 这句代码时,出现内存溢出的异常,求 ...