当前位置:首页 > CMS教程 > 其它CMS > 列表

Laravel5.1 框架登录和注册实现方法详解

发布:smiling 来源: PHP粉丝网  添加日期:2022-02-04 14:06:56 浏览: 评论:0 

本文实例讲述了Laravel5.1 框架登录和注册实现方法,分享给大家供大家参考,具体如下:

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

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

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

1 配置

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

  1. <?php 
  2. return [ 
  3.   /* 
  4.   |-------------------------------------------------------------------------- 
  5.   | Default Authentication Driver 
  6.   |-------------------------------------------------------------------------- 
  7.   | 
  8.   | This option controls the authentication driver that will be utilized. 
  9.   | This driver manages the retrieval and authentication of the users 
  10.   | attempting to get access to protected areas of your application. 
  11.   | 
  12.   | Supported: "database", "eloquent" 
  13.   | 
  14.   */ 
  15.   'driver' => 'eloquent'
  16.   /* 
  17.   |-------------------------------------------------------------------------- 
  18.   | Authentication Model 
  19.   |-------------------------------------------------------------------------- 
  20.   | 
  21.   | When using the "Eloquent" authentication driver, we need to know which 
  22.   | Eloquent model should be used to retrieve your users. Of course, it 
  23.   | is often just the "User" model but you may use whatever you like. 
  24.   | 
  25.   */ 
  26.   'model' => App\User::class
  27.   /* 
  28.   |-------------------------------------------------------------------------- 
  29.   | Authentication Table 
  30.   |-------------------------------------------------------------------------- 
  31.   | 
  32.   | When using the "Database" authentication driver, we need to know which 
  33.   | table should be used to retrieve your users. We have chosen a basic 
  34.   | default value but you may easily change it to any table you like. 
  35.   | 
  36.   */ 
  37.   'table' => 'users'
  38.   /* 
  39.   |-------------------------------------------------------------------------- 
  40.   | Password Reset Settings 
  41.   |-------------------------------------------------------------------------- 
  42.   | 
  43.   | Here you may set the options for resetting passwords including the view 
  44.   | that is your password reset e-mail. You can also set the name of the 
  45.   | table that maintains all of the reset tokens for your application. 
  46.   | 
  47.   | The expire time is the number of minutes that the reset token should be 
  48.   | considered valid. This security feature keeps tokens short-lived so 
  49.   | they have less time to be guessed. You may change this as needed. 
  50.   | 
  51.   */ 
  52.   'password' => [ 
  53.     'email' => 'emails.password'
  54.     'table' => 'password_resets'
  55.     'expire' => 60, 
  56.   ], 
  57. ]; 

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

2 创建路由

  1. /** 
  2.  * 用户认证 
  3.  */ 
  4. // getLogin 用于展示登录表单。 
  5. Route::get('/auth/login''Auth\AuthController@getLogin'); 
  6. // postLogin 用于提交用户登录数据。 
  7. Route::post('/auth/login''Auth\AuthController@postLogin'); 
  8. // getLogout 用于退出登录。 
  9. Route::get('/auth/logout''Auth\AuthController@getLogout'); 
  10. /** 
  11.  * 用户注册 
  12.  */ 
  13. // getRegister 用于展示注册表单。 
  14. Route::get('/auth/register''Auth\AuthController@getRegister'); 
  15. // postRegister 用于提交用户注册数据。 
  16. Route::post('/auth/register''Auth\AuthController@postRegister'); 

3 注册实现

3.1 编写视图

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

  1. <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4.   <meta charset="utf-8"
  5.   <title>用户注册</title> 
  6.   <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="external nofollow" rel="external nofollow" > 
  7. </head> 
  8. <body> 
  9. <div class="container"
  10.   <div class="row"
  11.     <div class="col-md-8 col-md-offset-2"
  12.       <div class="panel panel-default"
  13.         <div class="panel-heading">Register</div> 
  14.         <div class="panel-body"
  15.           <form action="{{ url('/auth/register') }}" method="post" role="form" class="form-horizontal"
  16.             <input type="hidden" name="_token" value="{{ csrf_token() }}"
  17.             <div class="form-group"
  18.               <label class="col-md-4 control-label">用户名:</label> 
  19.               <div class="col-md-6"
  20.                 <input type="text" name="name" class="form-control" autofocus> 
  21.               </div> 
  22.             </div> 
  23.             <div class="form-group"
  24.               <label class="col-md-4 control-label">邮箱:</label> 
  25.               <div class="col-md-6"
  26.                 <input type="email" name="email" class="form-control"
  27.               </div> 
  28.             </div> 
  29.             <div class="form-group"
  30.               <label class="col-md-4 control-label">密码:</label> 
  31.               <div class="col-md-6"
  32.                 <input type="password" name="password" class="form-control"
  33.               </div> 
  34.             </div> 
  35.             <div class="form-group"
  36.               <label class="col-md-4 control-label">确认密码:</label> 
  37.               <div class="col-md-6"
  38.                 <input type="password" name="password_confirmation" class="form-control"
  39.               </div> 
  40.             </div> 
  41.             <div class="form-group"
  42.               <div class="col-md-offset-4 col-md-8"
  43.                 <button type="submit" class="btn btn-primary">注册</button> 
  44.               </div> 
  45.             </div> 
  46.           </form> 
  47.         </div> 
  48.       </div> 
  49.     </div> 
  50.   </div> 
  51. </div> 
  52. </body> 
  53. </html> 

3.2 修改跳转URL

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

protected $redirectPath = '/';

4 登录实现

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

4.1 编写视图

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

  1. <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4.   <meta charset="utf-8"
  5.   <title>用户登录</title> 
  6.   <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="external nofollow" rel="external nofollow" > 
  7. </head> 
  8. <body> 
  9. <div class="container"
  10.   <div class="row"
  11.     <div class="col-md-8 col-md-offset-2"
  12.       <div class="panel panel-default"
  13.         <div class="panel-heading">Login</div> 
  14.         <div class="panel-body"
  15.           <form action="{{ url('/auth/login') }}" method="post" role="form" class="form-horizontal"
  16.             <input type="hidden" name="_token" value="{{ csrf_token() }}"
  17.             <div class="form-group"
  18.               <label class="col-md-4 control-label">邮箱:</label> 
  19.               <div class="col-md-6"
  20.                 <input type="email" name="email" class="form-control"
  21.               </div> 
  22.             </div> 
  23.             <div class="form-group"
  24.               <label class="col-md-4 control-label">密码:</label> 
  25.               <div class="col-md-6"
  26.                 <input type="password" name="password" class="form-control"
  27.               </div> 
  28.             </div> 
  29.             <div class="form-group"
  30.               <div class="col-md-offset-4 col-md-8"
  31.                 <button type="submit" class="btn btn-primary">登录</button> 
  32.               </div> 
  33.             </div> 
  34.           </form> 
  35.         </div> 
  36.       </div> 
  37.     </div> 
  38.   </div> 
  39. </div> 
  40. </body> 
  41. </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 检查用户是否登录

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

4.7 用于登录失败次数限制

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

5 登出用户

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

Auth::logout();

Tags: Laravel5.1登录 Laravel5.1注册

分享到: