Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-discover nested policies following conventional, parallel hierarchy #54493

Merged
merged 3 commits into from
Feb 6, 2025

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants