Replies: 1 comment 1 reply
-
What about custom file structures, for instance when the setup is domain driven? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everyone,
I'd like to propose a new feature that embraces modern PHP by adding support for attribute-based routing. This would serve as a powerful method that works in conjunction with the traditional file-based routing system, allowing developers to co-locate route definitions with their controller logic.
The Problem
Currently, route definitions in
routes/*.php
are physically separate from their controller logic. While this is a powerful and flexible system, in large applications, it can lead to frequent context switching between files and can sometimes make it harder to see the full picture of a controller's responsibilities at a glance.The Proposed Solution
I propose adding a new fluent method,
withAttributeRouting()
, to theApplicationBuilder
. To enable the feature, a developer would call this method inbootstrap/app.php
and provide the paths to be scanned.The feature is opt-in. If the method is not called, the feature remains completely disabled.
To be discovered, a controller would simply implement a marker interface,
AttributeRouteController
. Here is an example of what a controller would look like:Performance Considerations
We acknowledge that this feature may introduce a negligible performance impact of a few milliseconds in local development due to the file scanning process. However, this is completely mitigated in production. Once routes are cached with
php artisan route:cache
, there is zero performance overhead, leaving only the benefits of simplicity and a more enjoyable development experience.Key Features & Benefits
bootstrap/app.php
.--attributes
flag for themake:controller
command will be added to instantly scaffold a controller ready for attribute routing.Get
,Post
, etc.), helpers likeAny
andMatches
, and all standard route options (name
,middleware
,where
, etc.) are supported.Implementation Status
I have a full, working implementation of this feature ready, including tests. I've created this discussion to gather feedback from the community and the core team before submitting a Pull Request.
What are your thoughts on this proposal? I'm open to any feedback or suggestions.
Thanks for your time and consideration!
Beta Was this translation helpful? Give feedback.
All reactions