官方文檔:Laravel 5.2文檔服務——用戶認證php
若是你看官方文檔不太懂,那麼請看下文操做。html
框架版本:laravel 5.2 laravel
laravel 5.2內置了auth用戶認證服務,因此作網站時用戶認證彷佛變得簡單了不少,再加上框架的中間件機制,實現路由保護功能也更加方便快捷了。數據庫
laravel 框架源自外國人開發,許多思想和咱們的不太同樣,剛開始看的時候我仍是一頭霧水,可能接觸少的緣故;後來看了部分源碼才知道具體的過程,因此這裏寫篇教程幫助入門須要的朋友。瀏覽器
laravel 5.2內置了auth用戶認證服務,laravel有專門的命令來快速建立auth用戶認證的一些東西,好比路由註冊,默認視圖等。cookie
初始化(請開啓cmd命令行,cd到laravel框架目錄)app
php artisan make:auth
這個命令執行了的操做:composer
生成了註冊、登陸、重置密碼、主頁等視圖,請在 resources/views
文件夾中查看;
建立了HemoController.php文件,在 app/Http/Controllers
文件夾查看;
還有就是更新了路由,其實就是註冊了路由 app/Http/routes.php
,打開文件你會看到增長了兩行代碼:框架
Route::auth();
這一句是註冊相關的路由,具體的源碼能夠看 vendor/laravel/framework/srcIlluminate/Routing/Router.php
中的 auth
方法ide
Route::get('/home', 'HomeController@index');
這句註冊個home路由,指向Home控制器index方法,這是須要登錄的用戶才能訪問的路由,是由於Home控制器加了箇中間件 auth
。
以上是 php artisan make:auth
命令的詳細說明,接下來是數據庫建立。
若是你是經過composer安裝工具安裝的laravel框架,默認會在 database/migrations
目錄中存在兩個文件
保持默認就好,不用修改。
若是不存在這樣的文件,能夠經過php artisan make:migration create_users_table
和php artisan make:migration create_password_table
生成,
而後編輯文件
//create_users_table.php <?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateUsersTable extends Migration { public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); } public function down() { Schema::drop('users'); } }
//create_password_table.php <?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreatePasswordResetsTable extends Migration { public function up() { Schema::create('password_resets', function (Blueprint $table) { $table->string('email')->index(); $table->string('token')->index(); $table->timestamp('created_at'); }); } public function down() { Schema::drop('password_resets'); } }
到這裏請開啓數據庫,並在.env
文件配置好數據庫鏈接,建立相關數據;;而後執行命令建立表
php artisan migrate
命令會建立三個表
到這裏工做基本完畢,接下來看看登陸註冊效果。
cd到項目目錄,執行 php -S localhost:3000 -t public
,若是沒有錯誤,就能夠在瀏覽器輸入:localhost:3000
看效果了。
若是你發現開啓一致在轉圈圈,那是由於模板裏引用了cdn的boostrap文件,把它改爲爲本地的就快了。
點擊註冊
登陸
重置密碼,若是須要重置密碼,能夠在瀏覽器地址欄輸入 localhost:3000/password/reset
填寫須要重置密碼,點擊發送重置密碼連接到郵箱,這裏的郵箱是你註冊時的郵箱,點擊後會在郵箱裏收到信息
打開連接就能夠重置密碼了。
注:這裏發送郵件須要在config/email.php
中或者.env
文件配置郵箱服務,具體的能夠看官方文檔Laravel 5.2 服務——郵件
可能在流程中會用到一些查看驗證信息,因此一些必備的方法獲取認證信息是必要的。
一些與認證有關的方法
Auth::guard('admin') //指定看守 返回Auth對象 Auth::user(); //獲取經過驗證的用戶 Auth::user()->name Auth::check(); //檢查是否驗證 Auth::viaRemember(); //判斷用戶是否使用「記住我」cookie進行認證 Auth::login($user); //將一個已存在的用戶實例登陸到應用中,傳入實例必須是Illuminate\Contracts\Auth\Authenticatable 契約的實現 Auth::loginUsingId($userid);//經過用戶ID登陸到應用 Auth::once($credentials);//只在單個請求中將用戶登陸到應用,而不存儲任何 Session 和 Cookie Auth::attempt($credentials);//登陸用戶 ,$credentials是['email' => $email, 'password' => $password],這個方法會和數據庫對比 Auth::onceBasic(); Auth::provider(); Auth::logout(); //註銷驗證用戶 Auth::extend(); //自定義看守 Auth::provider(); //自定義用戶提供者
對於以上彷佛網上已經有答案,但我仍是發現了些問題,好比 laravel auth的源碼大部分已經寫死是經過 email,password字段來驗證的了;如何獲得解答我還在測試中,若是測試完成我會另開文檔編寫auth源碼解讀相關教程。若是你已經有了解決辦法,但願在評論告知下,謝謝。