Laravel 讓實現認證機制變得非常簡單。事實上,幾乎所有的設置默認就已經完成了。有關認證的配置文件都放在 config/auth.php 里,而在這些文件里也都包含了良好的注釋描述每一個選項的所對應的認證服務。
Laravel 默認在 app 文件夾內就包含了一個使用默認 Eloquent 認證驅動的 App/User模型。
注意:當為這個認證模型設計數據庫結構時,密碼字段至少有60個字符寬度。同樣,在開始之前,請先確認您的 users (或其他同義) 數據庫表包含一個名為 remember_token 長度為 100 的string類型、可接受 null 的字段。這個字段將會被用來儲存「記住我」的 session token。也可以通過在遷移文件中使用 $table-rememberToken(); 方法。 當然, Laravel 5 自帶的 migrations 里已經設定了這些字段。假如您的html' target='_blank'>應用程序并不是使用 Eloquent ,您也可以使用 Laravel 的查詢構造器做 database 認證驅動。
Laravel 已經預設了兩個認證相關的控制器。 AuthController 處理新的用戶注冊和「登陸」,而 PasswordController 可以幫助已經注冊的用戶重置密碼。
每個控制器使用 trait 引入需要的方法。在大多數應用上,你不需要修改這些控制器。這些控制器用到的視圖放在 resources/views/auth 目錄下。你可以依照需求修改這些視圖。
Laravel 自帶的認證包括:用戶注冊、登錄、密碼重置,也自帶整理了這幾個功能所需要的數據結構,位于:
database migrations 下,配置好數據庫后,執行所有遷移:
php artisan migrate
數據庫會自動生成:users(用戶表)、password_resets(重置密碼表)、migrations(遷移表)
要修改應用注冊新用戶時所用到的表單字段,可以修改 App/Services/Registrar 類。這個類負責驗證和建立應用的新用戶。
Registrar 的 validator 方法包含新用戶時的驗證規則,而 Registrar 的 create 方法負責在數據庫中建立一條新的 User 記錄。你可以自由的修改這些方法。Registrar 方法是通過AuthenticatesAndRegistersUsers trait 的中的 AuthController 調用的。
可以看一下源碼:
class Registrar implements RegistrarContract { /** * Get a validator for an incoming registration request. * * @param array $data * @return /Illuminate/Contracts/Validation/Validator */ public function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:users', 'password' => 'required|confirmed|min:6', ]); } /** * Create a new user instance after a valid registration. * * @param array $data * @return User */ public function create(array $data) { return User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}
如果你不想使用預設的 AuthController,你需要直接使用 Laravel 的身份驗證類來管理用戶認證。別擔心,這也很簡單的!首先,讓我們看看 attempt 方法:
<?php namespace App/Http/Controllers;use Auth;use Illuminate/Routing/Controller;class AuthController extends Controller { /** * Handle an authentication attempt. * * @return Response */ public function authenticate() { if (Auth::attempt(['email' => $email, 'password' => $password])) { return redirect()->intended('dashboard'); } }}
attempt 方法可以接受由鍵值對組成的數組作為第一個參數。password 的值會先進行 哈希。數組中的其他 值會被用來查詢數據表里的用戶。所以,在上面的示例中,會根據 email 列的值找出用戶。如果找到該用戶,會比對數據庫中存儲的哈希過的密碼以及數組中的哈希過后的 password值。假設兩個哈希后的密碼相同,會重新為用戶啟動認證通過的 session。
如果認證成功, attempt 將會返回 true。否則則返回 false。
** 注意:在上面的示例中,并不一定要使用 email 字段,這只是作為示例。你應該使用對應到數據表中的「username」的任何鍵值。
intended 方法會重定向到用戶嘗試要訪問的 URL , 其值會在進行認證過濾前被存起來。也可以給這個方法傳入一個預設的 URI,防止重定向的網址無法使用。
在認證過程中,你可能會想要加入額外的認證條件:
if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1])){ // The user is active, not suspended, and exists.}
如你說看到的,這個驗證中加入了一個active 字段的驗證。
假如你想要在應用中提供「記住我」的功能,你可以傳入布爾值作為 attempt 方法的第二個參數,這樣就可以保留用戶的認證身份(或直到他手動登出為止)。當然,你的 users 數據表必需包括一個字符串類型的 remember_token 列來儲存「記住我」的標識。
if (Auth::attempt(['email' => $email, 'password' => $password], $remember)){ // The user is being remembered...}
假如有使用「記住我」功能,可以使用 viaRemember 方法判定用戶是否擁有「記住我」的 cookie 來判定用戶認證:
if (Auth::viaRemember()){ //}
判斷一個用戶是否已經登錄,你可以使用 check 方法:
if (Auth::check()){ // The user is logged in...}
validate 方法可以讓你驗證用戶憑證信息而不用真的登陸應用
if (Auth::validate($credentials)){ //}
你也可以使用 once 方法來讓用戶在單一請求內登陸。不會有任何 session 或 cookie 產生:
if (Auth::once($credentials)){ //}
假如你需要將一個已經存在的用戶實例登陸應用,你可以調用 login 方法并且傳入用戶實例:
Auth::login($user);
當然,假設你使用 Laravel 內建的認證控制器,預設提供了讓用戶登出的方法。
Auth::logout();
<?php namespace App/Http/Controllers;use Illuminate/Routing/Controller;class ProfileController extends Controller { /** * Update the user's profile. * * @return Response */ public function updateProfile() { if (Auth::user()) { // Auth::user() returns an instance of the authenticated user... } }}
<?php namespace App/Http/Controllers;use Illuminate/Http/Request;use Illuminate/Routing/Controller;class ProfileController extends Controller { /** * Update the user's profile. * * @return Response */ public function updateProfile(Request $request) { if ($request->user()) { // $request->user() returns an instance of the authenticated user... } }}
<?php namespace App/Http/Controllers;use Illuminate/Routing/Controller;use Illuminate/Contracts/Auth/Authenticatable;class ProfileController extends Controller { /** * Update the user's profile. * * @return Response */ public function updateProfile(Authenticatable $user) { // $user is an instance of the authenticated user... }}
參考地址:http://laravel-china.org/docs/5.0/authentication
PHP編程鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答