-
Notifications
You must be signed in to change notification settings - Fork 11.5k
[12.x] Add attribute-based authorization for controller actions #56777
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
base: 12.x
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR introduces a new #[Authorize]
attribute for controller actions that provides declarative authorization without requiring manual Gate::authorize() calls inside methods.
- Adds
#[Authorize]
PHP attribute for method-level authorization - Implements automatic model parameter resolution from method signatures
- Integrates authorization checks into the ControllerDispatcher workflow
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
File | Description |
---|---|
src/Illuminate/Auth/Attributes/Authorize.php | Defines the new Authorize attribute class with ability and model parameters |
src/Illuminate/Routing/ControllerDispatcher.php | Implements authorization handling logic in the dispatch method |
tests/Auth/AuthorizeAttributeTest.php | Unit tests for the Authorize attribute instantiation |
tests/Routing/ControllerDispatcherAuthorizeAttributeTest.php | Tests for basic authorization scenarios with the dispatcher |
tests/Routing/AuthorizeAttributePolicyTest.php | Tests for policy-based authorization using the attribute |
tests/Routing/AuthorizeAttributeIntegrationTest.php | Integration tests covering edge cases and complex scenarios |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Updated the Authorize attribute to remove IS_REPEATABLE option.
Ok, I'll admit I was very anti-Attribute when they first started popping up. I was probably extra hesitant because of how poorly Annotations went, and at first it felt like the same hype that was pushed back then. However, I'm coming around to them, and definitely see their use in certain cases. One great example of a good use of Attributes is the Here, however, we're taking an incredibly simple 1 line method call ( We've also now got the additional burden of maintaining the Attribute. Some could argue it's not that big a deal, but it is a non-insignificant amount of code. It's not going to kill us, but it definitely contributes to death by 1000 cuts. sorry for the /rant TLDR: Attribute seems completely unnecessary here and not worth the additional code. |
@browner12 |
I actually really like the idea of an attribute for authorization. I noticed @browner12 mentioned you were very anti-Attribute at first, and I think maybe that’s part of where the pushback is coming from now. As @emargareten pointed out, Attributes in PHP aren’t just for swapping out a 1-liner or for “hiding” logic. They’re actually about attaching structured, machine-readable metadata to code — a way of expressing rules, configuration, or intent about a method or class, rather than putting everything into the main logic, meaning that if something is core to the flow of the method, it should stay there, but if it’s a rule or constraint — something external that should be easily discoverable, then an Attribute is a natural place for it. There have also been some recent discussions on twitter and in the community about where authorization logic should typically go, whether inside controller methods, in the routes file (as middleware), or in form request classes. Some people feel it’s not really the controller’s job to handle authorization; others point out that for certain methods like index or create, there isn’t always a dedicated form request, and routes/middleware don’t always cover everything clearly. From that perspective, having authorization declared as an Attribute looks like a very consistent, clean, and discoverable way to express access rules directly alongside the action, it centralizes the authorization intent without blending it into business logic, and without scattering it in multiple unrelated places. |
This PR adds a new
#[Authorize]
attribute that allows you to authorize controller actions instead of manually calling authorization inside the method.Examples: