raid/core-gate
Composer 安装命令:
composer require raid/core-gate
包简介
Raid Core Gate Package
README 文档
README
This package is responsible for handling all gates in the system.
Installation
composer require raid/core-gate
Configuration
php artisan core:publish-gate
Usage
class PostController extends Controller { /** * Invoke the controller method. */ public function __invoke(Post $post) { Post::gates('show', $post); // or using the authorize method. Post::gates()->authorize('show', $post); return response()->json([ 'resource' => $post, ]); } }
How to work this
Let's start with our gateable class ex:Post model.
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Raid\Core\Gate\Traits\Gate\Gateable; class Post extends Model { use Gateable; }
The gateable class must use Gateable trait.
To define our gates, we have two ways.
But remember if getGates method is defined, the config/gate.php gates will be ignored for this gateable class.
- Denfine
getGatesmethod in the gateable class, and define the gateable itself inconfig/gate.phpgateables.
<?php namespace App\Models; use App\Http\Gates\PostGate; use Illuminate\Database\Eloquent\Model; use Raid\Core\Gate\Traits\Gate\Gateable; class Post extends Model { use Gateable; /** * Get gateable gates. */ public static function getGates(): array { return [ // here we define our gate classes. PostGate::class, ]; } }
in config/gate.php gateables.
'gateables' => [ // here we define our gateable class. Post::class, ],
- or define gateable with gates in
config/gate.phpgates.
'gates' => [ // here we define our gateable class. Post::class => [ // here we define our gate classes. PostGate::class, ], ],
Gate
Now, let's create our gate class PostGate.
you can use this command to create the gate class.
php artisan core:make-gate PostGate
<?php namespace App\Http\Gates; use Raid\Core\Gate\Gates\Contracts\GateInterface; use Raid\Core\Gate\Gates\Gate; class PostGate extends Gate implements GateInterface { /** * {@inheritdoc} */ public const ACTIONS = []; }
The gate class must implement GateInterface interface.
The gate class must extend Gate class.
The gate class must define ACTIONS constant, which is the gate methods that will be defined with the Illuminate\Support\Facades\Gate class.
Let's define our gate first method.
<?php namespace App\Http\Gates; use App\Models\Post; use App\Models\User; use Raid\Core\Gate\Gates\Contracts\GateInterface; use Raid\Core\Gate\Gates\Gate; class PostGate extends Gate implements GateInterface { /** * {@inheritdoc} */ public const ACTIONS = [ 'show', ]; /** * Determine if the user can show the post. */ public function show(User $user, Post $post): bool { return $post->isPublic() || $user->isAdmin() || $user->isAuthor($post); } }
Great, now we can use the show method/action in the PostController.
<?php namespace App\Http\Controllers; use App\Models\Post; class PostController extends Controller { /** * Invoke the controller method. */ public function __invoke(Post $post) { Post::gates()->authorize('show', $post); return response()->json([ 'resource' => $post, ]); } }
The gates method is a static method that will be called from the gateable trait.
The authorize method is a method that will lead to call Illuminate\Support\Facades\Gate class authorize method.
The authorize method will throw Illuminate\Auth\Access\AuthorizationException exception if the gate method returns false.
The authorize method will return based on defined actions in the related gate class.
You can use the Illuminate\Support\Facades\Gate to authorize gateable actions like this:
<?php namespace App\Http\Controllers; use App\Models\Post; class PostController extends Controller { /** * Invoke the controller method. */ public function __invoke(Post $post) { Gate::authorize('post.show', $post); return response()->json([ 'resource' => $post, ]); } }
We can override the gateable class name by defining gateableName method in our class.
<?php namespace App\Models; use App\Http\Gates\PostGate; use Illuminate\Database\Eloquent\Model; use Raid\Core\Gate\Traits\Gate\Gateable; class Post extends Model { use Gateable; /** * Get gateable name. */ public static function gateableName(): string { return 'post'; } }
And that's it.
License
The MIT License (MIT). Please see License File for more information.
Credits
Security
If you discover any security-related issues, please email instead of using the issue tracker.
About Raid
Raid is a PHP framework created by Mohamed Khedr and it is maintained by Mohamed Khedr.
Support Raid
Raid is an MIT-licensed open-source project. It's an independent project with its ongoing development made possible.
raid/core-gate 适用场景与选型建议
raid/core-gate 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 43 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 08 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「core」 「gate」 「raid」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 raid/core-gate 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 raid/core-gate 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 raid/core-gate 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Contao Open Source CMS
Permission handling for Laravel using Gates with Route Names
Based on native Laravel's gates and policies. Hierarchical RBAC with callbacks.
DiZed Team Core module for the Magento 2 project
Core library that defines common interfaces used by the rest of the intahwebz..
统计信息
- 总下载量: 43
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 16
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-08-31