侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

Laravel  /  管理员 发布于 9年前   206

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

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

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

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

1 配置

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

 '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。

    用户注册  
Register

3.2 修改跳转URL

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

protected $redirectPath = '/';

4 登录实现

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

4.1 编写视图

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

    用户登录  
Login

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();

更多关于Laravel相关内容感兴趣的读者可查看本站专题:《Laravel框架入门与进阶教程》、《php优秀开发框架总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。

您可能感兴趣的文章:

  • Laravel 5.4重新登录实现跳转到登录前页面的原理和方法
  • Laravel5.2使用Captcha生成验证码实现登录(session巨坑)
  • laravel5.2实现区分前后台用户登录的方法
  • laravel5实现微信第三方登录功能
  • 基于Laravel5.4实现多字段登录功能方法示例
  • Laravel5.5 实现后台管理登录的方法(自定义用户表登录)
  • 解决laravel5中auth用户登录其他页面获取不到登录信息的问题
  • laravel 5.3 单用户登录简单实现方法
  • Laravel5.4框架使用socialite实现github登录的方法
  • Laravel 5.5 实现禁用用户注册示例


  • 上一条:
    Laravel5.1 框架文件管理操作实例分析
    下一条:
    Laravel5.1 框架分页展示实现方法实例分析
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • Laravel 11.14版本发布 - 新的字符串助手和ServeCommand改进(0个评论)
    • Laravel 11.12版本发布 - Artisan的`make`命令自动剪切`.php `扩展(0个评论)
    • Laravel的轻量型购物车扩展包:binafy/laravel-cart(37个评论)
    • Laravel 11.11版本发布 - 查看模型中的第三方关系:show(0个评论)
    • 近期文章
    • 在go语言中实现字符串可逆性压缩及解压缩功能(0个评论)
    • 使用go + gin + jwt + qrcode实现网站生成登录二维码在app中扫码登录功能(0个评论)
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-01
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客