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

[11.X] Introduces UsePolicy attribute #54409

Open
wants to merge 3 commits into
base: 11.x
Choose a base branch
from

Conversation

SimonBroekaert
Copy link

@SimonBroekaert SimonBroekaert commented Jan 30, 2025

Description

This pull request introduces a UsePolicy attribute that provides an alternative way to define a policy for a model using PHP 8.0+ attributes. This is useful for when your policy does not follow the naming conventions to automatically look up the correct policy. Even when the policy follows the naming convention, it may help developers by making it more clear that a specific policy is used and by making it more easy to navigate.

Usage

<?php

namespace App\Models\Foo;

use App\Policies\BarPolicy;
use Illuminate\Auth\Access\Attributes\UsePolicy;

#[UsePolicy(BarPolicy::class)]
class Foo extends Model
{
    // ...
}

Reasoning

This is inspired by the recent addition of the UseFactory attribute. It makes it easier to know which policy is used for a model, by defining it in the model class itself.

Priority

The code is executed after looking through the defined policies and right before the guessing logic.

Testing

The PR includes a test to verify the Policy is found.

Co-authored-by: Jeffrey Angenent <[email protected]>
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