您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關使用Laravel5.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。
/** * 用戶認證 */ // 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');
注冊視圖的路徑必須放在 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" 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">確認密碼:</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>
注冊后跳轉的URL有時候不是我們想要的,你可以自定義跳轉路由,在AuthController中添加即可:
protected $redirectPath = '/';
我們注冊后已經有了用戶了 現在可以試試登錄的實現了。
登錄的視圖路徑也是有規定的: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" 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>
登錄后的跳轉跟注冊后的跳轉是一樣的:
protected $redirectPath = '/';
當登錄失敗了Laravel會默認跳轉回 auth/login 路由,這也是可以自定義的:
protected $loginPath = '/error';
默認的登陸用戶名是郵箱,我們可以在AuthController中自定義:
// 該屬性默認為email,改成name是以用戶名作為賬號類型登錄。 protected $username = 'name';
我們可以通過Auth門面的方法來訪問已經登錄進來的用戶:
Auth::user()
if (Auth::check()) { // 這個用戶已經登錄... }
Laravel支持這種邏輯,我們只需要在AuthController中引入 ThrottlesLogins 這個trait 即可。一分鐘內登錄5次都不成功就會鎖閉一分鐘,它是基于 用戶名/郵箱和IP地址的。
我們只需要訪問 /auth/logout 就可以登出用戶了,當然還有一個方法 就是Auth門面方法:
Auth::logout();
以上就是使用Laravel5.1 框架怎么實現一個登錄和注冊功能,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。