Skip to content

Conversation

@jasonmccreary
Copy link
Contributor

A few times now I have read the docs and interpreted that if my models are under app/Models and my policies under app/Policies, my policies will be auto-discovered. This is true, except for my nested models and policies.

Currently, Laravel will not auto-discover the policy app/Policies/Nested/PostPolicy for the model app/Models/Nested/Post. This has to be registered explicitly

This PR resolves smooths out this exception of policy auto-discovery and removes the need for explicit registration for this conventional app structure.

While I don't see this to be a breaking change, I am targeting Laravel 12 as this discovers new paths and allows developers to remove explicit registration for models/policies using this conventional, parallel class hierarchy (optional).


Implementation Details
When the model class contains a Models sub-namespace two additional paths are added to auto-discovery: a sibling path of Policies and a subpath of Models/Policies.

This aligns with the algorithm outlined in the documentation, but anchors it to the model path. Since this is simply a string replacement, this should support both conventional structures as well as "modular" structures.

Examples:

  • App\Models\Auth\User auto-discovers App\Policies\Auth\UserPolicy or App\Models\Policies\Auth\UserPolicy
  • Some\Domain\Models\Auth\User auto-discovers Some\Domain\Policies\Auth\UserPolicy or Some\Domain\Models\Policies\Auth\UserPolicy
@taylorotwell taylorotwell merged commit 54f5cde into laravel:master Feb 6, 2025
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants