brighterly/laravel-dynamic-policies 问题修复 & 功能扩展

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

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

brighterly/laravel-dynamic-policies

Composer 安装命令:

composer require brighterly/laravel-dynamic-policies

包简介

Namespace-scoped Laravel policies per route group via setPolicies() macro

README 文档

README

Namespace-scoped Laravel policies per route group via a setPolicies() macro.

Laravel's default policy auto-discovery is global — one policy class per model across the whole app. This package lets you register different policies for the same model depending on which route group the request belongs to, using a simple fluent macro.

Requirements

  • PHP 8.2+
  • Laravel 11 or 12

Installation

composer require brighterly/laravel-dynamic-policies

The service provider is auto-discovered via Laravel's package auto-discovery.

Usage

1. Define a PoliciesMap for each namespace

Create an interface (or class) implementing Brighterly\LaravelPolicies\Contracts\PoliciesMap and declare the POLICIES_MAPPING constant:

// app/Policies/Tutor/TutorPoliciesMap.php
namespace App\Policies\Tutor;

use App\Models\Booking;
use App\Models\TutorTask;
use Brighterly\LaravelPolicies\Contracts\PoliciesMap;

final class TutorPoliciesMap implements PoliciesMap
{
    public const array POLICIES_MAPPING = [
        Booking::class  => BookingPolicy::class,
        TutorTask::class => TutorTaskPolicy::class,
    ];
}
// app/Policies/Customer/CustomerPoliciesMap.php
namespace App\Policies\Customer;

use App\Models\Booking;
use Brighterly\LaravelPolicies\Contracts\PoliciesMap;

final class CustomerPoliciesMap implements PoliciesMap
{
    public const array POLICIES_MAPPING = [
        Booking::class => BookingPolicy::class,
    ];
}

Both maps register a policy for Booking — but a different one per namespace.

2. Attach the map to a route group

Call ->setPolicies() before ->group() in your RouteServiceProvider:

use App\Policies\Customer\CustomerPoliciesMap;
use App\Policies\Tutor\TutorPoliciesMap;

Route::prefix('tutor')
    ->setPolicies(TutorPoliciesMap::POLICIES_MAPPING)
    ->group(base_path('routes/tutor.php'));

Route::prefix('cs')
    ->middleware('api')
    ->setPolicies(CustomerPoliciesMap::POLICIES_MAPPING)
    ->group(base_path('routes/customer_api.php'));

The macro works on both Route (fluent route builder) and RouteRegistrar (the object returned when you chain attributes like ->prefix(), ->middleware(), etc.).

3. Authorize as usual

Nothing changes in controllers or policies — use Laravel's standard authorization:

// in a controller
$this->authorize('update', $booking);

// or via Gate
Gate::authorize('view', $booking);

// or in Blade
@can('update', $booking)

The correct policy class is resolved based on which route group handled the request.

PhpStorm plugin

For the best experience you can use this plugin Custom Policy Navigator PhpStorm plugin

How it works

setPolicies() attaches the SetCustomPolicy middleware to the route group, encoding the [Model::class => Policy::class] pairs as middleware parameters. When a request hits a route in the group, the middleware calls Gate::policy($model, $policy) for each pair — overriding Laravel's global policy registry for that request only.

Optional: shared PoliciesMap contract in your app

If you want a shared base interface at App\Policies\Contracts\PoliciesMap (required by the Custom Policy Navigator PhpStorm plugin), publish the stub:

php artisan vendor:publish --tag=laravel-policies-stub

This creates app/Policies/Contracts/PoliciesMap.php extending the package interface. Your maps then implement App\Policies\Contracts\PoliciesMap instead of the package interface directly.

Local development

To use the package from a local path (e.g. alongside the app in a monorepo), add a path repository to composer.json:

"repositories": [
    {
        "name": "laravel-policies",
        "type": "path",
        "url": "../tools-laravel-policies-lib",
        "options": { "symlink": false }
    }
]

Use "symlink": false when the app runs inside Docker — symlinks to host paths are not accessible inside the container. After each change to the library, run composer update brighterly/laravel-policies inside the container.

License

MIT

brighterly/laravel-dynamic-policies 适用场景与选型建议

brighterly/laravel-dynamic-policies 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.08k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 brighterly/laravel-dynamic-policies 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-19