谁能帮助我了解何时/如何使用这些?
3 回答
我也想知道这一点,经过一番阅读,这就是我的结论:
中间件
通常在路由上运行(但您也可以在控制器方法上运行它)并可用于过滤或检查传入请求。
一个示例是auth,它确定尝试请求特定路由的人是否已通过身份验证(登录)到系统。另一个例子是检查请求是否具有特定的标头(例如,如果您想检查应用程序是否正在发送X-MYAPP-CUSTOMHEADER标头或其他内容)
如前所述,中间件可以定义在路由上(例如在web.php或api.php中)或在控制器中。
web.php 中的一个示例:
// Get all the posts, but only if the user has signed in to the website
Route::get('/posts', [PostController::class, 'index'])->middleware('auth');
中的一个例子PostController.php:
public function __construct() {
// Apply the `auth` middleware to every function in this controller
$this->middleware('auth');
// Apply the `signed` middleware, but only to the functions `download` and `delete`
$this->middleware('signed', ['only' => ['download', 'delete']]);
}
盖茨
Gates 是在您的AuthServiceProvider.php文件(在App\Providers文件夹中)中定义的函数,并指定用户可以做什么和不可以做什么。例如:
Gate::define('delete-post', function (User $user, Post $post) {
return $user->id === $post->user_id;
});
然后在你PostController的delete方法中:
public function delete(Request $request, Post $post)
{
if (Gate::denies('delete-post', $post)) { // Returns true if `$post->user_id` is not the same as `$user->id` (a.k.a the user is not allowed to delete this post)
abort(403);
}
$post->delete();
}
您还可以在刀片模板中使用一些辅助方法:
@can('delete-post', $post)
<!-- Show a link to the delete page here -->
@endcan
(我在下面对此进行了扩展)
警卫
Guards 是一种指定如何对请求进行身份验证的方法。
在我正在进行的一个项目中,jwt我的 API 路由上有一个(JSON Web 令牌)保护。这意味着当我执行类似的操作时auth()->attempt(['username' => 'test', 'password' => 'test']);,该auth()函数将尝试使用jwt警卫对我进行身份验证。
中指定使用哪个防护auth.php。我的目前看起来像这样:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users'
],
],
因此,在这种情况下,通过浏览器进入的请求将在登录时分配一个会话,通过 API 调用进入的请求将需要获取/提供令牌来进行身份验证。两者都将在验证时查找users表格以检查用户名/密码。
政策
策略的工作方式类似于门,但仅适用于特定模型并存储在它们自己的文件中(in App\Policies)
您可以使用php artisan make:policy PostPolicy -m Post. 它将创建一个名为的文件,该文件PostPolicy.php将创建一堆函数:
viewAny // Can the user even look at this model? If no, they'll be denied for all the below methods
view // Can the user look at the specified Post?
create // Can they create a new Post
update // Can they edit / update the specified Post?
delete // Can they (soft) delete the specified Post?
restore // Can they restore the specified (soft) deleted Post?
forceDelete // Can they force delete the specified Post?
典型的函数如下所示:
public function viewAny(User $user)
{
// If this returns true, then the other methods will be evaluated.
return $user->can_view_posts;
}
public function forceDelete(User $user, Post $post)
{
// If this returns true, then the user can force delete the post.
// This depends on viewAny being true
return $post->user_id == $user->id;
}
需要注意的一件事是,viewAny这与“查看全部”不同!想象viewAny成一栋建筑物的前门。如果viewAny为真,您可以进入大厅,但您不能查看任何房间,除非view某个特定房间也为真。
我相信您也可以将@can刀片模板与策略一起使用:
@can('forceDelete', $post)
<!-- Show button to force delete a post here -->
@endcan
反之亦然:
@cannot('view', $post)
<!-- Show a greyed out button or something -->
@cannot
以及@canany用于检查多个权限:
@canany(['edit', 'delete'])
<!-- Show something if the user can edit OR delete -->
@endcanany
结论
我希望这很有用。在阅读这篇文章时,我当然学到了很多东西。这比我想象的要多,所以值得查看 Laravel 文档,因为我在某些事情上可能是对的,但在其他事情上我可能会很糟糕。
Gate用于为登录用户访问添加逻辑
但策略 用于在模型访问级别添加逻辑
门的例子
Gate::define('update-post', function (User $user, Post $post) {
return $user->id === $post->user_id;
});
https://laravel.com/docs/8.x/authorization#writing-gates
在这里您可以在门中看到您的 gettin$user作为回调,因此它用于用户访问门
政策示例
跑
php artisan make:policy PostPolicy --model=Post
<?php
namespace App\Policies;
use App\Models\Post;
use App\Models\User;
class PostPolicy
{
/**
* Determine if the given post can be updated by the user.
*
* @param \App\Models\User $user
* @param \App\Models\Post $post
* @return bool
*/
public function update(User $user, Post $post)
{
return $user->id === $post->user_id;
}
}
https://laravel.com/docs/8.x/authorization#generating-policies
在这里你可以看到PostPolicy得到 2 个参数$user,$post这是模型级别的访问
Gate 用于为登录用户访问 添加逻辑 Policies 用于在模型访问级别添加逻辑