tetthys/cake
Composer 安装命令:
composer require tetthys/cake
包简介
Functional & Layered Authorization for Laravel — Declarative, Composable, Testable
README 文档
README
Functional & Layered Authorization for Laravel — Declarative, Composable, Testable
⚙️ Installation
composer require tetthys/cake
Laravel auto-discovers the service provider:
"extra": { "laravel": { "providers": [ "Tetthys\\Cake\\Integration\\Laravel\\CakeServiceProvider" ] } }
Once installed, Cake automatically registers:
@cakeCan/@cakeCannotBlade directivescakemiddleware- global helper
cakeCan()
🚀 Quick Usage
🧩 1. Define Rules
// app/Policies/PostRules.php namespace App\Policies; use Illuminate\Http\Request; use Tetthys\Cake\Rule\{Rule, RuleSet, Pred, Combinators as C}; final class PostRules { public function update(Request $request): RuleSet { $post = $request->route('post'); return new RuleSet([ new Rule( 'OwnerOrAdmin_WhenDraft', C::S_or( Pred::S(fn($u) => $u->id === $post->user_id), Pred::S(fn($u) => in_array('admin', $u->roles, true)) ), Pred::D(fn($u, $a, $o, $c) => $o->data->status === 'draft') ), ]); } }
- S → Subject condition (who)
- D → Domain condition (when/what)
- A
RuleSetis a list of(S ∧ D)rules. If any matches → Permit, otherwise → Deny (by default)
🧱 2. Controller Integration
Use the built-in trait AuthorizesRequest.
use Tetthys\Cake\Integration\Laravel\AuthorizesRequest; use App\Policies\PostRules; class PostController { use AuthorizesRequest; public function update(Request $request, Post $post) { $decision = $this->authorizeWithCake( $request, 'post.update', $post, app(PostRules::class)->update($request) ); // Decision implements isPermit() / isDeny() return response()->json(['ok' => $decision->isPermit()]); } }
🪶 3. Blade Directives
@cakeCan('post.update', $post) <button>✏️ Edit</button> @else <p>You cannot edit this post.</p> @endcakeCan @cakeCannot('post.update', $post) <p>❌ No permission</p> @endcakeCannot
✅ Automatically infers App\Policies\PostRules@update from action name + object type.
You can also pass:
- Explicit
"App\\Policies\\PostRules@update"string, or - A pre-built
RuleSetinstance.
⚡ 4. Middleware (Route-Level)
You can authorize before the controller executes.
// explicit Route::put('/posts/{post}', [PostController::class, 'update']) ->middleware('cake:post.update,App\\Policies\\PostRules@update');
or just use the shorthand — automatic inference:
// automatic inference -> App\Policies\PostRules@update Route::put('/posts/{post}', [PostController::class, 'update']) ->middleware('cake:post.update');
Cake finds your route model (
{post}), infers"App\Policies\PostRules@update", and denies (403) if no rule matches.
🧩 5. Helper Function
use function Tetthys\Cake\Integration\Laravel\cakeCan; if (cakeCan('post.update', $post)) { // Do something only if permitted }
This helper can take:
- action string (
'post.update') - model or object
- optional
RuleSetor"Class@method"string
If omitted, Cake will infer the policy automatically.
🧪 6. Testing
use Tetthys\Cake\Engine\Engine; use Tetthys\Cake\Model\{Actor, Action, ObjectRef, Context}; use App\Policies\PostRules; $engine = app(Engine::class); $decision = $engine->decide( new Actor('u-1', ['user']), new Action('post.update'), new ObjectRef('Post', (object)['user_id' => 'u-1', 'status' => 'draft']), new Context(), app(PostRules::class)->update(request()) ); expect($decision->isPermit())->toBeTrue();
Because rules are pure functions, you can test them without HTTP or Laravel context.
🧠 Key Features
| Feature | Description |
|---|---|
| Declarative | Express access as composable predicates, not if trees |
| Composable | Combine S and D with AND / OR / NOT |
| Secure | Deny-by-default — no implicit permits |
| Laravel-Ready | Works in controllers, Blade, middleware, and helpers |
| Functional | Stateless and testable, rule logic is pure PHP |
🧾 License
MIT © Tetthys
tetthys/cake 适用场景与选型建议
tetthys/cake 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 54 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 09 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「authorization」 「functional」 「laravel」 「Policy」 「rbac」 「access-control」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tetthys/cake 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tetthys/cake 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tetthys/cake 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Connection 'Db' codeception module to 'Yii2' module database settings
Functional library for php with proper currying
Ory-Hydra OAuth 2.0 Client Provider for The PHP League OAuth2-Client
Iteration tools for PHP
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
A PHP library with partial functions suitable for the pipe operator
统计信息
- 总下载量: 54
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-17