定制 lukeraymonddowning/nightguard 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱: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.

lukeraymonddowning/nightguard 适用场景与选型建议

lukeraymonddowning/nightguard 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 34 次下载、GitHub Stars 达 11, 最近一次更新时间为 2021 年 03 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 lukeraymonddowning/nightguard 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lukeraymonddowning/nightguard 我们能提供哪些服务?
定制开发 / 二次开发

基于 lukeraymonddowning/nightguard 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

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