[Laravel] 08 - Auth & Data Migration

登陸註冊框架


1、加載Auth模塊 

Step 1, 安裝Auth模塊php

生成相關 laravel 框架內部的代碼模塊:html

$ php artisan make:auth

 

  • 自動添加了路由代碼

在laravel狀況下,routes/web.php內多了auth相關的代碼:laravel

對應的Route的代碼:【追根溯源】git

    /**
     * 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: startergithub

 

  • 自動添加了視圖代碼

 

Step 2, 手動改變樣式web

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

[2] 修改相關代碼數據庫

[3] 效果展現閉包

 

 

 

登陸註冊實現


1、鏈接數據庫

  • 配置.env 文件

 

  • 用戶與密碼

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

此時,須要配置好模型,包括相應的數據庫內容。

[2] 執行migration命令 

$ php artisan migrate

建立好了 Migration table,包括:

1. users_table

2. password_resets_table

[3] 多出來三張表,以下所示。

[4] 到此,登陸和註冊功能生效!

 

 

2、Laravel中的 數據遷移

  • 概念與理解

Migration方便於團隊開發,它就像 數據庫的版本控制同樣,它的功能就是能夠 和別人共享你的數據庫結構

使用Laravel後,建立一張表的時候就直接生成遷移;一個遷移文件對應一張表。

 

Ref: [Laravel 5 教程學習筆記] 7、數據庫遷移 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

 

 

3、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
相關文章
相關標籤/搜索