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

[12.X] Introduces UsePolicy attribute #54409

Closed

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 <1571879+devfrey@users.noreply.github.com>
@taylorotwell taylorotwell marked this pull request as draft February 12, 2025 16:31
@SimonBroekaert
Copy link
Author

@taylorotwell , May I know the reason why this PR has been put on Draft? It's not clear to me if I have to make any code changes or not.

Thanks!

@phh
Copy link
Contributor

phh commented Feb 24, 2025

At least I know that Taylor wont look at draft PRs. So if you consider this one is ready for review I think you can just put it out of draft :)

@SimonBroekaert SimonBroekaert changed the title [11.X] Introduces UsePolicy attribute [12.X] Introduces UsePolicy attribute Feb 25, 2025
@SimonBroekaert SimonBroekaert marked this pull request as ready for review February 25, 2025 00:07
@SimonBroekaert SimonBroekaert changed the base branch from 11.x to 12.x February 25, 2025 00:08
@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

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.

None yet

4 participants