PHPackages                             mud-qadm/policy-discovery - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. mud-qadm/policy-discovery

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mud-qadm/policy-discovery
=========================

Laravel package for recursive policy auto discovery supporting modular architecture

v1.0.0(1mo ago)02MITPHPPHP ^8.1

Since Jun 11Pushed 1mo agoCompare

[ Source](https://github.com/AhmedSabryH/policy-discovery)[ Packagist](https://packagist.org/packages/mud-qadm/policy-discovery)[ RSS](/packages/mud-qadm-policy-discovery/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (2)Versions (2)Used By (0)

Laravel Policy Discovery
========================

[](#laravel-policy-discovery)

[![Latest Version on Packagist](https://camo.githubusercontent.com/dc4b87d2800c904a1a23357bc7833c35f2cfbbca64296d61fe04b5a8916fdfa6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d75642d7161646d2f706f6c6963792d646973636f766572792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mud-qadm/policy-discovery)[![Total Downloads](https://camo.githubusercontent.com/8616a6ca6f9c7885de8e980fbea4f7ee5652d198733a0840123bf61d2d441c5c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d75642d7161646d2f706f6c6963792d646973636f766572792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mud-qadm/policy-discovery)[![PHP Version](https://camo.githubusercontent.com/d9ecb92453804f00dc5eead846fd98e071096e6908d0da93253451388a002449/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6d75642d7161646d2f706f6c6963792d646973636f766572792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mud-qadm/policy-discovery)[![Laravel Version](https://camo.githubusercontent.com/9e8f792b7eca5e9c7edf13729e3623e54ca8c1c1efca32a3f89dfe342a78e447/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d31302e7825323025374325323031312e7825323025374325323031322e7825323025374325323031332e782d626c75653f7374796c653d666c61742d737175617265)](https://laravel.com)[![License](https://camo.githubusercontent.com/aa88fa99af71be11eb92f11ad9dccb0c58dfd60eb1327d154e13942e7fd4bae2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6d75642d7161646d2f706f6c6963792d646973636f766572792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mud-qadm/policy-discovery)

Automatically discover and register Laravel policies recursively – even in deeply nested directories or modular domain structures.

📋 Table of Contents
-------------------

[](#-table-of-contents)

- [The Problem](#-the-problem)
- [Solution](#-solution)
- [Features](#-features)
- [Requirements](#-requirements)
- [Installation](#-installation)
- [Configuration](#-configuration)
- [Usage](#-usage)
- [Artisan Commands](#artisan-commands)
- [How It Works](#how-it-works)
- [Caching &amp; Performance](#-caching--performance)
- [Troubleshooting](#-troubleshooting)
- [License](#-license)

---

🔥 The Problem
-------------

[](#-the-problem)

Laravel's native policy auto-discovery works perfectly for flat directory structures:

```
app/Policies/
├── UserPolicy.php
├── PostPolicy.php
└── CommentPolicy.php

```

But real-world applications often grow beyond that. When you organise policies by domain or module:

```
app/
└── Policies/
    ├── Settings/
    │   ├── RolePolicy.php
    │   └── PermissionPolicy.php
    ├── HR/
    │   └── EmployeePolicy.php
    └── Finance/
        └── InvoicePolicy.php

```

Laravel **does not** recursively scan subdirectories. This forces you to manually register every policy using `Gate::policy()` – a tedious and error-prone process.

---

💡 Solution
----------

[](#-solution)

This package recursively scans any number of policy directories, automatically maps policies to their corresponding models, and registers them with Laravel's Gate – **without any manual intervention**.

- ✅ Recursively discovers policies in nested folders
- ✅ Maps policies to models using naming conventions or explicit annotations
- ✅ Works with Laravel's package auto-discovery
- ✅ Includes file-based caching for production performance
- ✅ Provides artisan commands for inspection and cache management

---

✨ Features
----------

[](#-features)

FeatureDescription**Recursive discovery**Finds policies in any subdirectory under configured paths**Automatic model mapping**Infers model from policy name or uses `@model` annotation**Modular support**Perfect for DDD, modules, or domain-driven structures**Zero configuration**Drop-in installation, works out of the box**Performance caching**File-based cache to avoid filesystem scanning on every request**CLI tools**List, validate, warmup, clear, rebuild, and optimise caches**Export mappings**Export discovered policy–model mappings to JSON**Diagnostics**Statistics and validation commands to ensure correct setup**Automatic policy detection**Newly created policies are discovered automatically without manual registration---

🧰 Requirements
--------------

[](#-requirements)

- PHP 8.1 or higher
- Laravel 10.x, or higher

---

📦 Installation
--------------

[](#-installation)

Install via Composer:

```
composer require mud-qadm/policy-discovery
```

Laravel will automatically register the package's service provider thanks to [Laravel's package discovery](https://laravel.com/docs/packages#package-discovery).

To publish the configuration file:

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

This will create `config/policy-discovery.php` where you can customise directories, caching behaviour, and naming conventions.

---

⚙️ Configuration
----------------

[](#️-configuration)

After publishing, you can modify the following options in `config/policy-discovery.php`:

```
return [

    /*
    |--------------------------------------------------------------------------
    | Policy Base Directory
    |--------------------------------------------------------------------------
    | Root directory where all policy classes are located.
    */
    'policy_directory' => app_path('Policies'),

    /*
    |--------------------------------------------------------------------------
    | Policy Namespace
    |--------------------------------------------------------------------------
    | Base namespace used to resolve policy classes.
    */
    'policy_namespace' => 'App\\Policies',

    /*
    |--------------------------------------------------------------------------
    | Model Directories
    |--------------------------------------------------------------------------
    | Directories used for automatic model resolution when mapping policies.
    */
    'model_directories' => [
        app_path('Models'),
    ],

    /*
    |--------------------------------------------------------------------------
    | Recursive Scanning
    |--------------------------------------------------------------------------
    | When enabled, policies inside nested directories will be discovered.
    */
    'recursive' => true,

    /*
    |--------------------------------------------------------------------------
    | Cache Enabled
    |--------------------------------------------------------------------------
    | Enable or disable caching of discovered policy mappings.
    */
    'cache_enabled' => env('POLICY_DISCOVERY_CACHE', true),

    /*
    |--------------------------------------------------------------------------
    | Cache File Path
    |--------------------------------------------------------------------------
    | File used to store cached policy-model mappings.
    */
    'cache_file' => storage_path('policy-discovery/mapping.json'),

];
```

---

🚀 Usage
-------

[](#-usage)

Once installed, simply place your policy files anywhere under the configured directories.

### Example

[](#example)

**Policy file:** `app/Policies/Settings/RolePolicy.php`

```
