Authorization
Authorization
要在 Livewire 中对动作进行鉴权,您可以在任何组件中使用AuthorizesRequests trait,然后像平常在控制器内部一样调用 $this->authorize()
。 例如:
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class EditPost extends \Livewire\Component
{
use AuthorizesRequests;
public $post;
public function mount(Post $post)
{
$this->post = $post;
}
public function save()
{
$this->authorize('update', $this->post);
$this->post->update(['title' => $this->title]);
}
}
如果您使用其他 guard 对用户进行身份验证,则还要在 livewire 配置文件中将 guard 添加到 middleware_group 中:
...
'middleware_group' => ['web', 'auth:otherguard'],
...