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

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

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

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

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

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。

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

3.2 修改跳转URL

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

protected $redirectPath = '/';

4 登录实现

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

4.1 编写视图

登录的视图路径也是有规定的: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>

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程序设计有所帮助。

(0)

相关推荐

  • 基于Laravel5.4实现多字段登录功能方法示例

    前言 最近在一个项目中需要实现一个多字段登录功能,简单来说就是可以使用用户名.邮箱或手机号任意一种方式进行登录.所以本文就来给大家介绍了关于Laravel5.4多字段登录的相关内容,分享出来供大家参考学习,话不多说了,来一起看看详细的介绍吧. 以下内容基于laravel5.4 方法如下: 首先,通过artisan工具生成auth模块 php artisan make:auth 这时候App\Http\Controllers目录下会新增一个Auth目录,该目录下为注册登录相关的控制器,resour

  • 解决laravel5中auth用户登录其他页面获取不到登录信息的问题

    首先创建user表,里面有:id, name, password,remember_token等字段. 然后再Models添加表模型User.php <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use DB; class User extends Model

  • laravel5.2实现区分前后台用户登录的方法

    1.前台登录 直接使用laravel自带的auth php artisan make:auth 然后可以查看路由文件: Route::group(['middleware' => 'web'], function () { Route::auth(); Route::get('/home', 'HomeController@index'); }); 执行php artisan migrate 会发现生成了两张表. 2.后台登录 编辑配置文件 config\auth.php 添加guards中的a

  • Laravel 5.5 实现禁用用户注册示例

    在app/Http/Controllers/Auth/RegisterController.php中重写两个函数, 如下: /** * Show the application registration form. * * @return \Illuminate\Http\Response */ public function showRegistrationForm() { return redirect('login'); } /** * Handle a registration requ

  • Laravel5.5 实现后台管理登录的方法(自定义用户表登录)

    最近群里很多人文档,laravel如何做会员和管理两个身份登录,今天把教程分享一下 自定义用户表登录 认证是由 guards 和 providers 两部分构成的, defaults 配置是默认选择一个 guard 认证驱动,所以我们在这两个配置项中分别添加一个 admin 和 admins 选项. <?php 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' =

  • laravel5实现微信第三方登录功能

    背景 最近手头一个项目需要实现用户在网站的第三方登录(微信和微博),后端框架laravel5.4. 实现过程以微信网页版第三方登录,其他于此类似,在此不做重复. 准备工作 网站应用微信登录是基于OAuth2.0协议标准构建的微信OAuth2.0授权登录系统. 在进行微信OAuth2.在进行微信OAuth2.0授权登录接入之前,在微信开放平台注册开发者帐号,并拥有一个已审核通过的网站应用,并获得相应的AppID和AppSecret,申请微信登录且通过审核后,可开始接入流程. 总结下来就是: 1.进

  • laravel 5.3 单用户登录简单实现方法

    需求描述 当前用户只能在一个地方登录,即同一账号不能再2个以上窗口登录,后登录者踢出前登录者. 设计思路 在用户数据表中新增1个字段,记录当前登录用的session_id,当用户登录的时候把session_id存储到数据库中,然后在中间件认证里加判断,判断当前用户sesson_id是否和数据库中的session_id相等,如果相等继续向下执行,反之注销登录. 具体实现 1.首先在用户表中新增字段last_session,类型CHAR 40长度 2.在AuthController 控制器中实现父类

  • Laravel 5.4重新登录实现跳转到登录前页面的原理和方法

    前言 本文主要给大家介绍的是关于Laravel5.4重新登录跳转到登录前页面的相关内容,分享出来供大家参考学习,下面话不多说,来一起看看详细的介绍: 一.应用场景: 用户登陆后存在过期时间,超时用户需重新登录.例:当用户在/user/2 页面,登陆过期后跳转到登陆页面,登陆后用户还应在/user/2而不是home/index. 二.实现原理 在判断用户过期后,存储用户当前的url地址到session中,下次登陆后跳转到此url地址. 三.laravel中的具体实现 路由中间件(判断登陆状态) 这

  • Laravel5.4框架使用socialite实现github登录的方法

    本文实例讲述了Laravel5.4框架使用socialite实现github登录的方法.分享给大家供大家参考,具体如下: 1.安装laravel5.4 composer create-project laravel/laravel zcms 5.4 2.安装Socialite composer require laravel/socialite 3.配置 编辑config/app.php 'providers' => [ // 其它服务提供者... Laravel\Socialite\Socia

  • Laravel5.2使用Captcha生成验证码实现登录(session巨坑)

    最近有朋友要我帮忙弄一下laravel的验证码登陆,所以稍稍研究了一下.(本人都快忘了咋使用laravel了) 首先,安装laravel就不用在下赘述了吧,我的版本是5.2.45(注:laravel5.2.6以上的版本中间件可以自动加载),这还是挺重要的. 安装完成之后,你需要使用composer来加载你的Captcha,具体方法就是在你的composer.json中的require数组中加上"gregwar/captcha":"1.*"这行代码.然后嘞,就在你的项

随机推荐