lukeraymonddowning/nightguard 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lukeraymonddowning/nightguard

Composer 安装命令:

composer require lukeraymonddowning/nightguard

包简介

Set up Laravel Eloquent guards in seconds

README 文档

README

Set up Auth guards using Eloquent in seconds.

Unit Tests

Introduction

Laravel guards provide a super convenient way of authorizing different areas of your application based on the type of your user. For a lot of projects, gates and permissions suffice, but guards go one step further by allowing you to use dedicated eloquent models for each area of authentication.

However, guards can be a little confusing to set up, especially if you're new to it all. The documentation is thorough, but because of the low level nature of guards, it can be overwhelming.

That's why Nightguard was created. Rather than you having to dig into config files and jump from file to file, you can have a guard set up around your own custom Eloquent model with a single line of code!

Installation

You can install Nightguard via composer:

composer require lukeraymonddowning/nightguard

Usage

Imagine we want an admin panel for our application. We want to have a dedicated administrators table, and an Administrator Eloquent model. Only administrators should be allowed into the admin panel. Even if a User model is logged in under the default guard, they should not have access to any admin route.

Let's start by creating a model and migration for our administrators. Nightguard provides a super convenient Artisan command that will create a model and migration with all the columns you'll need for authentication:

php artisan nightguard:model Administrator

That was simple!

With that out of the way, all that's left is to set up the guard. Head in to your AuthServiceProvider, and add the following code to your boot method:

public function boot() 
{
    Nightguard::create(App\Models\Administrator::class);
}

...and voilà! You've successfully registered a gate that only an authenticated Administrator can access.

If you create a route protected by your new 'administrator' gate, it will be protected:

Route::get('example-url', fn() => 'Super secret!')->middleware('auth:administrator');

The name of the guard is set automatically based on the class name of the model you pass to the create method. The guard name will always be singular and kebab-cased.

Nightguard Facade

The Nightguard Facade includes the following methods:

create($model, $guard = null)

This is how you register a new Eloquent guard. The first parameter should be the class name of your eloquent model (eg: App\Models\Administrator::class).

The second parameter is entirely optional and is only needed if you want to use a custom guard name. When omitted, Nightguard will guess the guard name based on your model name.

You should place these method calls in the boot method of one of your Service Providers.

Commands

Nightguard provides the following Artisan commands for you to use:

php artisan nightguard:model YourModelName

This command allows you to quickly scaffold files for your new Authenticatable Eloquent model. The database will have all the required columns, and the model will extend the correct classes and have the desired traits and casts out of the box.

统计信息

  • 总下载量: 34
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 11
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-03-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固