登录注册框架


一、加载Auth模块

Step 1, 安装Auth模块

生成相关 laravel 框架内部的代码模块:

$ php artisan make:auth
  • 自动添加了路由代码

在laravel情况下,routes/web.php内多了auth相关的代码:

对应的Route的代码:【追根溯源】

    /**
* Register the typical authentication routes for an application.
*
* @return void
*/
public function auth()
{
// Authentication Routes...
$this->get('login', 'Auth\AuthController@showLoginForm');
$this->post('login', 'Auth\AuthController@login');
$this->get('logout', 'Auth\AuthController@logout'); // Registration Routes...
$this->get('register', 'Auth\AuthController@showRegistrationForm');
$this->post('register', 'Auth\AuthController@register'); // Password Reset Routes...
$this->get('password/reset/{token?}', 'Auth\PasswordController@showResetForm');
$this->post('password/email', 'Auth\PasswordController@sendResetLinkEmail');
$this->post('password/reset', 'Auth\PasswordController@reset');
}
  • HomeController类的构造函数
public function __contruct()
{
  $this->middleware('auth');

中间件,请见:[Laravel] 14 - REST API: starter

  • 自动添加了视图代码

Step 2, 手动改变样式

[1] 找到 resources/views/layouts/app.blade.php。

[2] 修改相关代码

[3] 效果展示

登录注册实现


一、连接数据库

  • 配置.env 文件

  • 用户与密码

[1] 配置好,只是连接好了,但不能用。

此时,需要配置好模型,包括相应的数据库内容。

[2] 执行migration命令

$ php artisan migrate

创建好了 Migration table,包括:

1. users_table

2. password_resets_table

[3] 多出来三张表,如下所示。

[4] 到此,登录和注册功能生效!

二、Laravel中的 数据迁移

  • 概念与理解

Migration方便于团队开发,它就像 数据库的版本控制一样,它的功能就是可以 和别人共享你的数据库结构。

使用Laravel后,创建一张表的时候就直接生成迁移;一个迁移文件对应一张表。

Ref: [Laravel 5 教程学习笔记] 七、数据库迁移 Migrations

迁移是一种数据库的版本控制。可以让团队在修改数据库结构的同时,保持彼此的进度一致。它是 Laravel 5 最强大的功能之一。

一般我们可以通过phpmyadmin或者Navicat等数据库管理工具来创建、修改数据表结构。

如果就自己一个人的话还好,但是如果团队中有多个人,我们就需要导出表结构,然后传给团队中其他人,他们再把数据表结构导入他们的数据库,这时如果表中原来有数据的话就可能出现一些问题。

而 Laravel 5 中的 Migrations 很好的避免了此问题。

一个migration类包含两个方法 updown

    • up中主要包含创建表的具体内容。

    • down中和前者相反,删除表。

    • Schema::create 接受两个参数。第一个是你要创建表的表名;第二个是一个闭包(匿名函数),获取用于定义新表的 Blueprint 对象。

  • 生成 迁移文件

生成迁移文件:create_students_table.php

$ php artisan make:migration create_user_table --create=users

生成迁移文件在目录:[./database/migrations/...]

class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
} /**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');  // 删除一张表
}
}
  • 生成模型 附带 迁移文件

创建Model的时候,同时创建Migration文件。

$ php artisan make:model Article -m

生成:<timm>_create_articles_table.php 迁移文件
  • 完善 迁移文件

Complete migration file like this:

public function up()
{
  Schema::create('students', function (Blueprint $table)     $table->increment('id');
    $table->string('name');
    $table->integer('age')->unsigned()->default(0);
    $table->integer('sex')->unsigned()->default(10);
    $table->integer('created_at')->default(0);
    $table->integer('updated_at')->default(0);
  });
}

based on this sql table as following:

  • 执行 迁移文件

根据迁移文件,在数据库创建对应的数据表。

php artisan migration

三、Laravel中的 数据填充

  • 概念与理解

当我们创建好表结构后,通常都要生成一些测试用的数据来测试。为了应对这个场景,Laravel 提供了相当好的服务 --seed

Laravel 的 seeder 都会放在:/database/seeders 目录中,

并且会提供一个DatabaseSeeder 在 DatabaseSeeder的run 方法中。

Ref: Laravel5.1 填充数据库

之前已经有了表,现在当然需要有个“概念”来处理表中的数据,也就是“数据填充”。

在run中可以用构建器 也可以用模型工厂。

  • 基本操作

  • 生成 填充文件
$ php artisan make:seeder StudentTableSeeder

在 laravel 中提供了一个解决方案, 我们可以创建额外的填充器来专门针对一张表。

生成了seeds文件夹内的文件: StudentTableSeeder.php

[1] 内容如下:【通过构造器操作数据库】

class StudentTableSeeder extends Seeder
{
  public function run()
  {
    DB::table('students')->insert([
      ['name' => 'sean', 'age' => 18],
      ['name' => 'immoc', 'age' => 20],
  }
}

[2] 或者使用 “模型工厂填充”。

Ref: laravel数据库迁移详解

方法一和我们以前用sql语句插入一条数据是一样的效果,所以并没有多大的提升。

我们想追求的是一次性填充多条测试数据,那么我们可以使用方法二。

手动指定每一个模型填充的属性是很笨重累赘的,在laravel中取而代之,我们可以使用模型工厂来方便地生成大量的数据库记录。

class StudentTableSeeder extends Seeder
{
  public function run()
  {
    factory(\App\User::class, 10)->create();
  }
}

另一个实例,基于 https://github.com/fzaninotto/Faker

<?php
/**
* 运行数据库填充
* @return void
*/
$factory->define(App\User::class, function (Faker\Generator $faker) {
static $password;
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => str_random(10),
]; });
  • 执行 填充文件

指定执行

执行 ArticlesSeeder 这个填充器 可以这样写:

php artisan db:seed --class=StudentTableSeeder

默认执行

默认执行就是执行 DatabaseSeeder 这个填充器:

php artisan db:seed

[Laravel] 08 - Auth & Data Migration的更多相关文章

  1. Core Data Migration 之拆分Entity

    参考文章:http://blog.slalom.com/2010/01/08/splitting-an-entity-in-a-coredata-migration/ 最近自己做的背单词软件,在之前的 ...

  2. laravel 删除一条migration后要执行composer命令

    Laravel 删除一条migration 字数29 阅读30 评论0 喜欢0 如果迁移已经执行,先回滚php artisan migrate:rollback 然后删除迁移文件,运行composer ...

  3. Laravel 之Auth用户认证

    (1)生成Auth所需文件 打开phpstorm的命令行: php artisan make:auth 生成成功后,打开web.php, 发现多了如下代码: Auth::routes(); Route ...

  4. django 1.7 新特性 --- data migration

    官方文档:https://docs.djangoproject.com/en/dev/topics/migrations/ 1.7 之前大家可能会用south用于管理数据库的模型的同步.1.7之后dj ...

  5. laravel的auth用户认证的例子

    参考http://www.cnblogs.com/yjf512/p/4042356.html 需要注意的是,生成的测试数据,password部分必须用laravel自带的hash一下 Hash::ma ...

  6. laravel 验证码 auth方式登录 中间件判断session是否存在

    首先下载laravel的插件 composer下载  实现验证码       composer require mews/captcha 在config/app.php进行配置 'providers' ...

  7. Entity Framework mvc Code First data migration

    1. Code First 可以先在代码里写好数据模型,自动生成DB.下一次启动的时候会根据__MigrationHistory判断 数据库是否和模型一致. 详情参考:http://blogs.msd ...

  8. php的laravel数据库版本管理器migration

    第一步:连接数据库 打开.env文件.配置DB_HOST DB_PORT DB_DATABASE=LARAVEL DB_USERNAME DB_PASSWORD 注意DB_DATABASE这一项需要自 ...

  9. DMA(Data Migration Assistant)迁移SQLServer数据库

    DMA适用于 本地SQLServer向Azure SQL Database迁移 两台不同的数据库服务器之间迁移 高版本->低版本 或 低版本->高版本 本文以两台不同服务器的低版本(SQL ...

随机推荐

  1. mysql的密码忘记了怎么办

    我们的大脑不是电脑,有些东西难免会忘,但是会了这个再也不担心宝宝忘记密码了 (1)点击开始/控制面板/服务/mysql-->右击mysql看属性,里面有mysql的安装地址,然后找到安装地址进行 ...

  2. Zookeeper简介说明

    Zookeeper(一)简介说明 1.1 什么是Zookeeper? Zookeeper是一个高效的分布式协调服务,它暴露了一些公用服务,比如命名/配置管理/同步控制/群组服务等.我们可以使用ZK来实 ...

  3. ScrollView不能包含多个子项,ScrollView can host only one direct child

    http://debuglog.iteye.com/blog/1441828 ScrollView不能包含多个子项,ScrollView can host only one direct child ...

  4. Directx11代码下载

    很多年前的代码,看还有朋友需要,上传到百度网盘了 https://pan.baidu.com/s/1pnGFt84htvdXeK86pvyR8Q https://pan.baidu.com/s/1zT ...

  5. SharedPreferences 原理 源码 进程间通信 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  6. 使用lightProbe来模拟动态物体的照明shader

    VertexLit path中读取lightProbe烘焙信息: Shader "VertexLitProbe" { Properties { _MainTex ("Ba ...

  7. 解剖SQLSERVER 第一篇 数据库恢复软件商的黑幕(有删减版)

    解剖SQLSERVER 第一篇  数据库恢复软件商的黑幕(有删减版) 这一系列,我们一起来解剖SQLSERVER 在系列的第一篇文章里本人可能会得罪某些人,但是作为一位SQLSERVER MVP,在我 ...

  8. Linux使用图形LVM(Logical Volume Manager)工具进行分区的动态扩展

  9. C#-MVC开发微信应用(7)--在管理系统中同步微信用户分组信息

    在前面几篇文章中,逐步从原有微信的API封装的基础上过渡到微信应用平台管理系统里面,逐步介绍管理系统中的微信数据的界面设计,以及相关的处理操作过程的逻辑和代码.希望从一个更高的层次介绍微信的开发. 在 ...

  10. TitleBar 的那些设置

    设置状态栏透明: View decorView = activity.getWindow().getDecorView(); int option = View.SYSTEM_UI_FLAG_LAYO ...