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'); }
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] 配置好,只是链接好了,但不能用。app
此时,须要配置好模型,包括相应的数据库内容。
[2] 执行migration命令
$ php artisan migrate
建立好了 Migration table,包括:
1. users_table
2. password_resets_table
[3] 多出来三张表,以下所示。
[4] 到此,登陆和注册功能生效!
Migration方便于团队开发,它就像 数据库的版本控制同样,它的功能就是能够 和别人共享你的数据库结构。
使用Laravel后,建立一张表的时候就直接生成迁移;一个迁移文件对应一张表。
Ref: [Laravel 5 教程学习笔记] 7、数据库迁移 Migrations
迁移是一种数据库的版本控制。能够让团队在修改数据库结构的同时,保持彼此的进度一致。它是 Laravel 5 最强大的功能之一。
通常咱们能够经过phpmyadmin或者Navicat等数据库管理工具来建立、修改数据表结构。
若是就本身一我的的话还好,可是若是团队中有多我的,咱们就须要导出表结构,而后传给团队中其余人,他们再把数据表结构导入他们的数据库,这时若是表中原来有数据的话就可能出现一些问题。
而 Laravel 5 中的 Migrations 很好的避免了此问题。
一个migration
类包含两个方法 up &
down
。
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 提供了至关好的服务 --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