Adds support for Attribute-based Routing (MVP) #56424
Draft
+625
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request introduces the foundational support for attribute-based routing in Laravel. It provides a clean, modern, and developer-friendly way to declare routes directly on controller methods.
Motivation
As PHP continues to evolve, attributes have become a powerful tool for declarative programming. Many modern frameworks have adopted them for routing, which helps co-locate route definitions with their corresponding logic, improving clarity and developer ergonomics. This PR aims to bring this same modern convenience to Laravel, embracing modern PHP capabilities more deeply.
Crucially, this system is designed to work in conjunction with the traditional file-based routing system, not as a replacement. Developers can use both methods side-by-side in the same application, giving them more flexibility in how they organize their projects.
Proposed Solution
The feature is opt-in and enabled via a new fluent method,
withAttributeRouting()
, inbootstrap/app.php
.Developers can easily specify which directories should be scanned for
web
andapi
route groups. If called with no arguments, it uses a sensible default.Controllers are marked for discovery by implementing the
AttributeRouteController
interface. Routes and groups are then defined with simple attributes:Performance Considerations
The performance impact of this feature has been carefully considered. In local development, a negligible overhead of a few milliseconds is introduced by the file scanning process. However, in production, once routes are cached using
php artisan route:cache
, there is zero performance impact. The only change is a simpler and more enjoyable development workflow.This is an MVP
This initial pull request implements the Minimum Viable Product (MVP) for this feature. The goal is to first agree on the fundamental architecture and API.
I have already locally developed and tested more advanced features that can be added in subsequent PRs upon approval of this core concept. These future possibilities include:
#[Resource]
and#[ApiResource]
attributes for full RESTful controller support.#[Group]
features like only and except for targeting specific methods.scopeBindings
support for both resources and groups.implicit
discovery mode to reduce boilerplate.route:list
andmake:controller
).Next Steps & Collaboration
I am fully committed to seeing this feature through to completion. If this initial proposal is accepted by the team and the community, I am ready to collaborate on any necessary changes and submit the subsequent PRs to build out the full feature set.