Laravel 速查表
显示全部 1. Artisan 2. Auth 3. Blade 4. Cache 5. Collection 6. Composer 7. Config 8. Container 9. Cookie 10. DB 11. Environment 12. Event 13. File 14. Helper 15. Input 16. Lang 17. Log 18. Mail 19. Model 20. Pagination 21. Queue 22. Redirect 23. Request 24. Response 25. Route 26. SSH 27. Schema 28. Security 29. Session 30. Storage 31. String 32. URL 33. UnitTest 34. Validation 35. View
Menu

Auth

未匹配的标注
本文档最新版为 9.x,旧版本可能放弃维护,推荐阅读最新版!

用户认证

// 获取 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');

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 0
发起讨论 只看当前版本


暂无话题~