[TOC]
## Auth
### 用戶認證
```
// 獲取 Auth 對象,等同于 Auth Facade
auth();
// 判斷當前用戶是否已認證(是否已登錄)
Auth::check();
// 判斷當前用戶是否未登錄,與 check() 相反
Auth::guest();
// 自定義看守器 默認為 `web`
Auth::guard();
// 獲取當前的認證用戶
Auth::user();
// 獲取當前的認證用戶的 ID(未登錄情況下會報錯)
Auth::id();
// 通過給定的信息來嘗試對用戶進行認證(成功后會自動啟動會話)
Auth::attempt(['email' => $email, 'password' => $password]);
// 通過 Auth::attempt() 傳入 true 值來開啟 '記住我' 功能
Auth::attempt($credentials, true);
// 注冊嘗試登錄的事件監聽器
Auth::attempting($callback);
// 只針對一次的請求來認證用戶
Auth::once($credentials);
// 使用 ID 登錄,無 Cookie 和會話登錄
Auth::onceUsingId($id);
// 登錄一個指定用戶到應用上
Auth::login(User::find(1), $remember = false);
// 檢測是否記住了登錄
Auth::viaRemember();
// 登錄指定用戶 ID 的用戶到應用上
Auth::loginUsingId(1, $remember = false);
// 使用戶退出登錄(清除會話)
Auth::logout();
// 清除當前用戶的其他會話
Auth::logoutOtherDevices('password', $attribute = 'password');
// 驗證用戶憑證
Auth::validate($credentials);
// 使用 HTTP 的基本認證方式來認證
Auth::basic('username');
// 執行「HTTP Basic」登錄嘗試,只認證一次
Auth::onceBasic();
// 發送密碼重置提示給用戶
Password::remind($credentials, function($message, $user){});
```
### 用戶授權
```
// 定義權限
Gate::define('update-post', 'Class@method');
Gate::define('update-post', function ($user, $post) {...});
// 傳遞多個參數
Gate::define('delete-comment', function ($user, $post, $comment) {});
// 一次性的定義多個 Gate 方法
Gate::resource('posts', 'App\Policies\PostPolicy');
// 檢測權限是否被定義
Gate::has('update-post');
// 檢查權限
Gate::denies('update-post', $post);
Gate::allows('update-post', $post);
Gate::check('update-post', $post);
// 指定用戶進行檢查
Gate::forUser($user)->allows('update-post', $post);
// 在 User 模型下,使用 Authorizable trait
User::find(1)->can('update-post', $post);
User::find(1)->cannot('update-post', $post);
User::find(1)->cant('update-post', $post);
// 攔截所有檢查,返回 bool
Gate::before(function ($user, $ability) {});
// 設置每一次驗證的回調
Gate::after(function ($user, $ability, $result, $arguments) {});
// Blade 模板語法
@can('update-post', $post)
@endcan
// 支持 else 表達式
@can('update-post', $post)
@else
@endcan
// 無權限判斷
@cannot
@endcannot
// 生成一個新的策略
php artisan make:policy PostPolicy
php artisan make:policy PostPolicy --model=Post
// `policy` 幫助函數
policy($post)->update($user, $post)
// 控制器授權
$this->authorize('update', $post);
// 指定用戶 $user 授權
$this->authorizeForUser($user, 'update', $post);
// 控制器的 __construct 中授權資源控制器
$this->authorizeResource(Post::class, 'post');
// AuthServiceProvider->boot() 里修改策略自動發現的邏輯
Gate::guessPolicyNamesUsing(function ($modelClass) {
// 返回模型對應的策略名稱,如:// 'App\Model\User' => 'App\Policies\UserPolicy',
return 'App\Policies\\'.class_basename($modelClass).'Policy';
});
// 中間件指定模型實例
Route::put('/post/{post}', function (Post $post) { ... })->middleware('can:update,post');
// 中間件未指定模型實例
Route::post('/post', function () { ... })->middleware('can:create,App\Post');
```
- 入門指南
- 安裝
- 部署
- 基礎功能
- 路由
- 中間件
- CSRF 保護
- 控制器
- 請求
- 響應
- 視圖
- URL
- Session
- 表單驗證
- 錯誤
- 日志
- 前端開發
- Blade 模板
- 本地化
- 腳手架
- 編譯資源 Mix
- 安全相關
- 用戶認證
- API 認證
- 綜合話題
- 命令行
- 廣播
- 緩存
- 集合
- 事件
- 文件存儲
- 輔助函數
- 郵件發送
- 消息通知
- 擴展包開發
- 隊列
- 任務調度
- 數據庫
- 快速入門
- 查詢構造器
- 分頁
- 數據庫遷移
- 數據填充
- Redis
- Eloquent ORM
- 快速入門
- 速查表
- Artisan
- Auth
- Blade
- Cache
- Collection
- Composer
- Config
- Container
- Cookie
- DB
- Environment
- Event
- File
- Helper
- Input
- Lang
- Log
- Model
- Pagination
- Queue
- Redirect
- Request
- Response
- Route
- SSH
- Schema
- Security
- Session
- Storage
- String
- URL
- UnitTest
- Validation
- View