Laravel5.1 框架登錄和注冊(cè)實(shí)現(xiàn)方法詳解
本文實(shí)例講述了Laravel5.1 框架登錄和注冊(cè)實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
關(guān)于登錄和注冊(cè) Laravel自帶了一套組件實(shí)現(xiàn)了這一功能,我們只需要實(shí)現(xiàn)簡(jiǎn)單的視圖即可。
AuthController是專門管理用戶注冊(cè)和登錄的。
PassWordController是重置密碼用的,今天暫不做記錄。
1 配置
我們可以在 config/auth.php 文件中進(jìn)行用戶認(rèn)證的配置:
<?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, ], ];
這是默認(rèn)的配置,注釋寫的很清楚了 如果有特別需要可以做更改,一般情況中我們使用默認(rèn)的就OK。
2 創(chuàng)建路由
/** * 用戶認(rèn)證 */ // getLogin 用于展示登錄表單。 Route::get('/auth/login', 'Auth\AuthController@getLogin'); // postLogin 用于提交用戶登錄數(shù)據(jù)。 Route::post('/auth/login', 'Auth\AuthController@postLogin'); // getLogout 用于退出登錄。 Route::get('/auth/logout', 'Auth\AuthController@getLogout'); /** * 用戶注冊(cè) */ // getRegister 用于展示注冊(cè)表單。 Route::get('/auth/register', 'Auth\AuthController@getRegister'); // postRegister 用于提交用戶注冊(cè)數(shù)據(jù)。 Route::post('/auth/register', 'Auth\AuthController@postRegister');
3 注冊(cè)實(shí)現(xiàn)
3.1 編寫視圖
注冊(cè)視圖的路徑必須放在 views/auth/ 目錄中 并命名為 register.blade.php。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>用戶注冊(cè)</title> <link rel="stylesheet" rel="external nofollow" rel="external nofollow" > </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">確認(rèn)密碼:</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">注冊(cè)</button> </div> </div> </form> </div> </div> </div> </div> </div> </body> </html>
3.2 修改跳轉(zhuǎn)URL
注冊(cè)后跳轉(zhuǎn)的URL有時(shí)候不是我們想要的,你可以自定義跳轉(zhuǎn)路由,在AuthController中添加即可:
protected $redirectPath = '/';
4 登錄實(shí)現(xiàn)
我們注冊(cè)后已經(jīng)有了用戶了 現(xiàn)在可以試試登錄的實(shí)現(xiàn)了。
4.1 編寫視圖
登錄的視圖路徑也是有規(guī)定的:views/auth/ 然后命名為:login.balde.php
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>用戶登錄</title> <link rel="stylesheet" rel="external nofollow" rel="external nofollow" > </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 登錄后跳轉(zhuǎn)
登錄后的跳轉(zhuǎn)跟注冊(cè)后的跳轉(zhuǎn)是一樣的:
protected $redirectPath = '/';
4.3 登錄失敗跳轉(zhuǎn)
當(dāng)?shù)卿浭×薒aravel會(huì)默認(rèn)跳轉(zhuǎn)回 auth/login 路由,這也是可以自定義的:
protected $loginPath = '/error';
4.4 修改登錄用戶名
默認(rèn)的登陸用戶名是郵箱,我們可以在AuthController中自定義:
// 該屬性默認(rèn)為email,改成name是以用戶名作為賬號(hào)類型登錄。 protected $username = 'name';
4.5 查看用戶信息
我們可以通過(guò)Auth門面的方法來(lái)訪問(wèn)已經(jīng)登錄進(jìn)來(lái)的用戶:
Auth::user()
4.6 檢查用戶是否登錄
if (Auth::check()) { // 這個(gè)用戶已經(jīng)登錄... }
4.7 用于登錄失敗次數(shù)限制
Laravel支持這種邏輯,我們只需要在AuthController中引入 ThrottlesLogins 這個(gè)trait 即可。一分鐘內(nèi)登錄5次都不成功就會(huì)鎖閉一分鐘,它是基于 用戶名/郵箱和IP地址的。
5 登出用戶
我們只需要訪問(wèn) /auth/logout 就可以登出用戶了,當(dāng)然還有一個(gè)方法 就是Auth門面方法:
Auth::logout();
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
上一篇:Laravel5.1 框架分頁(yè)展示實(shí)現(xiàn)方法實(shí)例分析
欄 目:PHP編程
下一篇:Laravel5.1 框架數(shù)據(jù)庫(kù)查詢構(gòu)建器用法實(shí)例詳解
本文標(biāo)題:Laravel5.1 框架登錄和注冊(cè)實(shí)現(xiàn)方法詳解
本文地址:http://mengdiqiu.com.cn/a1/PHPbiancheng/10971.html
您可能感興趣的文章
- 01-11thinkphp框架類庫(kù)擴(kuò)展操作示例
- 01-11關(guān)于Yii2框架跑腳本時(shí)內(nèi)存泄漏問(wèn)題的分析與解決
- 01-11php 實(shí)現(xiàn)簡(jiǎn)單的登錄功能示例【基于thinkPHP框架】
- 01-11Laravel框架Blade模板簡(jiǎn)介及模板繼承用法分析
- 01-11Laravel 微信小程序后端實(shí)現(xiàn)用戶登錄的示例代碼
- 01-11Laravel框架基礎(chǔ)語(yǔ)法與知識(shí)點(diǎn)整理【模板變量、輸出、include引入
- 01-11Laravel框架Eloquent ORM刪除數(shù)據(jù)操作示例
- 01-11Laravel框架Eloquent ORM修改數(shù)據(jù)操作示例
- 01-11Laravel框架Eloquent ORM簡(jiǎn)介、模型建立及查詢數(shù)據(jù)操作詳解
- 01-11laravel5.1框架下的批量賦值實(shí)現(xiàn)方法分析


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02php本站才可以請(qǐng)求數(shù)據(jù) php本地?cái)?shù)據(jù)庫(kù)
- 04-02關(guān)于txt數(shù)據(jù)庫(kù)php的信息
- 04-02php打印請(qǐng)求數(shù)據(jù) php打印輸出結(jié)果
- 04-02網(wǎng)頁(yè)里php操作數(shù)據(jù)庫(kù) php網(wǎng)頁(yè)例子
- 04-02php插入數(shù)據(jù)庫(kù)為亂碼 php連接數(shù)據(jù)庫(kù)亂
- 04-02php數(shù)據(jù)庫(kù)地址 phpstudy 數(shù)據(jù)庫(kù)
- 04-02php數(shù)據(jù)庫(kù)數(shù)據(jù)相加 php數(shù)據(jù)庫(kù)添加數(shù)據(jù)
- 04-02數(shù)據(jù)權(quán)限架構(gòu)思路php 數(shù)據(jù)權(quán)限設(shè)計(jì)方
- 04-02php數(shù)據(jù)庫(kù)輸入變量 php里輸出數(shù)據(jù)庫(kù)數(shù)
- 04-02php如何用導(dǎo)入數(shù)據(jù) php用來(lái)導(dǎo)入其他文
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 04-02jquery與jsp,用jquery
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11Mac OSX 打開(kāi)原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?