eneadm/ladder
Composer 安装命令:
composer require eneadm/ladder
包简介
Feather light permissions for Laravel.
关键字:
README 文档
README
Ladder 🪜
Ladder simplifies role and permission management for your Laravel project by avoiding storing everything in the database. Inspired by Laravel Jetstream, it offers a static approach, reducing queries and ensuring immutability for easy modifications.
Install
This package requires Laravel 12 and above.
composer require eneadm/ladder
Once Ladder is installed, create a new LadderServiceProvider to manage roles and permissions. You can do so effortlessly with this command:
php artisan ladder:install
Lastly, execute the migration command to create a single pivot user_role table, assigning roles to users.
php artisan migrate
Use
Before using Ladder add the HasRoles trait to your App\Models\User model.
By doing so this trait will provide the necessary methods to manage roles and permissions.
use Ladder\HasRoles; class User extends Authenticatable { use HasRoles; }
HasRoles trait in detail
// Access all of user's roles... $user->roles : Illuminate\Database\Eloquent\Collection // Determine if the user has the given role... $user->hasRole($role) : bool // Access all permissions for a given role belonging to the user... $user->rolePermissions($role) : array // Access all permissions belonging to the user... $user->permissions() : Illuminate\Support\Collection // Determine if the user role has a given permission... $user->hasRolePermission($role, $permission) : bool // Determine if the user has a given permission... $user->hasPermission($permission) : bool
All method arguments can accept string, array, Collection or Enum if desired. For optimal performance, it is advisable to use array or Collection as arguments when handling multiple entries.
Roles & Permissions
Users can receive roles with permissions defined in App\Providers\LadderServiceProvider using Ladder::role method. This involves specifying a role's slug, name, permissions, and description. For instance, in a blog app, role definitions could be:
Ladder::role('admin', 'Administrator', [ 'post:read', 'post:create', 'post:update', 'post:delete', ])->description('Administrator users can perform any action.'); Ladder::role('editor', 'Editor', [ 'post:read', 'post:create', 'post:update', ])->description('Editor users have the ability to read, create, and update posts.');
Assign Roles
You may assign roles to the user using the roles relationship that is provided by the Ladder\HasRoles trait:
use App\Models\User; $user = User::find(1); $user->roles()->updateOrCreate(['role' => 'admin']);
Authorization
For request authorization, utilize the Ladder\HasRoles trait's hasPermission method to check user's role permissions. Generally, verifying granular permissions is more important than roles. Roles group permissions and are mainly for presentation. Use the hasPermission method within authorization policies.
/** * Determine whether the user can update a post. */ public function update(User $user, Post $post): bool { return $user->hasPermission('post:update'); }
Wildcard Permissions
Ladder supports wildcard permissions for more flexible permission management:
*- Grants access to all permissions*:create- Grants access to all create permissions (e.g.,post:create,user:create)*:update- Grants access to all update permissions (e.g.,post:update,user:update)
Ladder::role('super-admin', 'Super Administrator', [ '*', // Full access to everything ])->description('Super Administrator users can perform any action.'); Ladder::role('content-manager', 'Content Manager', [ '*:create', // Can create any resource '*:update', // Can update any resource 'post:read', ])->description('Content Manager users can create and update any content.');
Viewing Roles and Permissions
You can display a visual table of all roles and their permissions using the ladder:show command:
php artisan ladder:show
The command supports different table styles:
php artisan ladder:show default php artisan ladder:show borderless php artisan ladder:show compact php artisan ladder:show box
This will display a matrix showing which permissions are assigned to each role, with ✔ and ✖ indicators.
License
Ladder is free software distributed under the terms of the MIT license.
eneadm/ladder 适用场景与选型建议
eneadm/ladder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25.16k 次下载、GitHub Stars 达 261, 最近一次更新时间为 2023 年 08 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「roles」 「permissions」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 eneadm/ladder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 eneadm/ladder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 eneadm/ladder 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Multiauth package
User permissions for MoonShine
This package provides a flexible way to add Role-based Permissions to Laravel 6.x
This package provides a flexible way to add Role-based Permissions to Laravel
Library for advanced filesystem functions
Role-based Access Control: Roles, Permissions and ACL
统计信息
- 总下载量: 25.16k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 261
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-08-15