首页 > 作文

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

更新时间:2023-04-08 03:42:49 阅读: 评论:0

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

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

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

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

1 配置

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

<?phpreturn [  /*  |--------------------------------------------------------------------------  | default authentication driver  |--------------------------------------------------------------------------  |  | this option controls the authentication driver that will be utilized.  | this driver manages the retrieval and authentication of the urs  | attempting to get access to protected areas of your application.  |  | supported: "databa", "eloquent"  |  */  'driver' => 'eloquent',  /*  |--------------------------------------------------------------------------  | authentication model  |--------------------------------------------------------------------------  |  | when using the "eloquent" authentication driver, we need to know which  | eloquent model should be ud to retrieve your urs. of cour, it  | is often just the "ur" model but you may u whatever you like.  |  */  'model' => app\ur::class,  /*  |--------------------------------------------------------------------------  | authentication table  |--------------------------------------------------------------------------  |  | when using the "databa" authentication driver, we need to know which  | table should be ud to retrieve your urs. we have chon a basic  | default value but you may easily change it to any table you like.  |  */  'table' => 'urs',  /*  |--------------------------------------------------------------------------  | password ret ttings  |--------------------------------------------------------------------------  |  | here you may t the options for retting passwords including the view  | that is your password ret e-mail. you can also t the name of the  | table that maintains all of the ret tokens for your application.  |  | the expire time is the number of minute感谢有你 老师s that the ret token should be  | considered valid. this curity feature keeps tokens short-lived so  | they have less time to be guesd. you may change this as needed.  |  */  'password' => [    'email' => 'emails.password',    'table' => 'password_rets',    '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 chart="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-offt-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">              <labbring的过去式el 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-offt-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 chart="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-offt-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-offt-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 登录失败跳转

当登录失败了l见习期工作总结aravel会默认跳转回 auth/login 路由,这也是可以自定义的:

protected $loginpath = '/error';

4.4 修改登录用户名

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

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

4.5 查看用户信息

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

auth::ur()

4.6 检查用户是否登录

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

4.7 用于登录失败次数限制

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

5 登出用户

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

auth::logout();

本文发布于:2023-04-08 03:42:47,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/ce4e56addaf17c866c8640c2d682ad40.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:Laravel5.1 框架登录和注册实现方法详解.doc

本文 PDF 下载地址:Laravel5.1 框架登录和注册实现方法详解.pdf

标签:跳转   用户   视图   用户注册
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图