PHPackages                             jundayw/laravel-policy - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. jundayw/laravel-policy

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

jundayw/laravel-policy
======================

The Laravel Policy.

v5.0.0(3y ago)02.2kMITPHPPHP &gt;=7.2.0

Since Jul 10Pushed 1y ago1 watchersCompare

[ Source](https://github.com/jundayw/laravel-policy)[ Packagist](https://packagist.org/packages/jundayw/laravel-policy)[ RSS](/packages/jundayw-laravel-policy/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (3)Used By (0)

安装方法
====

[](#安装方法)

命令行下, 执行 composer 命令安装:

```
composer require jundayw/laravel-policy

```

[![Latest Stable Version](https://camo.githubusercontent.com/add63f4617512c5792b355fee70ba3ef86ad7e896b7bb22af61f450df19c12aa/68747470733a2f2f706f7365722e707567782e6f72672f6a756e646179772f6c61726176656c2d706f6c6963792f76)](https://packagist.org/packages/jundayw/laravel-policy)[![Total Downloads](https://camo.githubusercontent.com/06d3fbe6393de8222e1361fd6f0d12f7b51a66eb223d7a497d49df1cb17b2306/68747470733a2f2f706f7365722e707567782e6f72672f6a756e646179772f6c61726176656c2d706f6c6963792f646f776e6c6f616473)](https://packagist.org/packages/jundayw/laravel-policy)[![Latest Unstable Version](https://camo.githubusercontent.com/04ad34f213a9d07f48b095bc3993048c4a02d71ce2063978291eea7173be324a/68747470733a2f2f706f7365722e707567782e6f72672f6a756e646179772f6c61726176656c2d706f6c6963792f762f756e737461626c65)](https://packagist.org/packages/jundayw/laravel-policy)[![License](https://camo.githubusercontent.com/d73d095dff75dc8846f9bb38bbb708f49586d5b86f3e49139d274a968c934784/68747470733a2f2f706f7365722e707567782e6f72672f6a756e646179772f6c61726176656c2d706f6c6963792f6c6963656e7365)](https://packagist.org/packages/jundayw/laravel-policy)[![PHP Version Require](https://camo.githubusercontent.com/baa848f1f8fff05154149bd7f17e8ca1575f1f8aab76ace2780c8936d384e753/68747470733a2f2f706f7365722e707567782e6f72672f6a756e646179772f6c61726176656c2d706f6c6963792f726571756972652f706870)](https://packagist.org/packages/jundayw/laravel-policy)

使用方法
====

[](#使用方法)

Model
-----

[](#model)

需要验证权限的 `App\Models\User` 继承 `Jundayw\LaravelPolicy\PolicyContract`， 并实现 `getPolicies(string $ability, mixed $arguments)` 方法，返回如下类型的权限数组

```
namespace App\Models;

use Jundayw\LaravelPolicy\PolicyContract;
use Jundayw\LaravelPolicy\Policy;

class Manager extends Authenticate implements PolicyContract
{
    use Policy;

    /**
     * @param string $ability
     * @param mixed $arguments
     * @return string[]
     */
    public function getPolicies(string $ability, mixed $arguments): array
    {
        // do anything for get polices
        return [
            // "backend.module.list",
            // "backend.module.create",
            // "backend.module.store",
            // "backend.module.edit",
            // "backend.module.update",
            // "backend.module.destroy",
            // "backend.policy.list",
            // "backend.policy.create",
            // "backend.policy.store",
            // "backend.policy.edit",
            // "backend.policy.update",
            // "backend.policy.destroy",
            // "backend.role.*",
            // "backend.*.*",
        ];
    }
}
```

使用内置中间件
-------

[](#使用内置中间件)

控制器中使用

```
class AuthController extends CommonController
{
    public function __construct()
    {
        $this->middleware('policy');
    }
}
```

或者路由中使用

```
Route::middleware('policy')->group(function(){});
```

内置中间件验证中，若无权访问将抛出 `Jundayw\LaravelPolicy\Exceptions\PolicyException` 异常，注意捕获

自定义中间件
------

[](#自定义中间件)

```
use Jundayw\LaravelPolicy\Policies\Policy;
/**
 * Handle an incoming request.
 *
 * @param Request $request
 * @param Closure $next
 * @return Response|PolicyException
 */
public function handle(Request $request, Closure $next)
{
    // 传入当前请求需要的权限标识符，可自定义任意形式
    // 如
    // admin.role.delete
    // admin/role/delete
    // admin-role-delete
    // adminRoleDelete
    // ...
    // 注意与 getPolicies 方法返回类型一致
    $policy = '';
    if ($request->user()->can($policy, [Policy::class])) {
        return $next($request);
    }
    // 自定义无权访问行为
    throw new Exception('UnAuthorized Access.');
}
```

调试模式
====

[](#调试模式)

发布配置文件
------

[](#发布配置文件)

```
php artisan vendor:publish --tag=artisan-policy-config
```

配置 `.env` 文件，追加

```
POLICY_ENABLED=false
```

配置为 `false` 关闭权限验证，默认为 `true`

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance31

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~241 days

Total

2

Last Release

1161d ago

Major Versions

v1.0.0 → v5.0.02023-03-09

### Community

Maintainers

![](https://www.gravatar.com/avatar/2da9b458375a1b7972b7c4d26a5bf8f3e48db305e8805da36f253956f33c5568?d=identicon)[jundayw](/maintainers/jundayw)

---

Top Contributors

[![jundayw](https://avatars.githubusercontent.com/u/16873970?v=4)](https://github.com/jundayw "jundayw (6 commits)")

---

Tags

laravelpolicylaravelPolicy

### Embed Badge

![Health badge](/badges/jundayw-laravel-policy/health.svg)

```
[![Health](https://phpackages.com/badges/jundayw-laravel-policy/health.svg)](https://phpackages.com/packages/jundayw-laravel-policy)
```

###  Alternatives

[dlnsk/h-rbac

Based on native Laravel's gates and policies. Hierarchical RBAC with callbacks.

378.3k](/packages/dlnsk-h-rbac)[deefour/authorizer

Simple Authorization via PHP Classes

483.3k](/packages/deefour-authorizer)[ingria/laravel-x509-auth

Laravel 5 Client Certificate auth middleware

375.6k](/packages/ingria-laravel-x509-auth)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
