feat(router): add php 8 attributes support for route definition#349
Conversation
|
Why did you avoid environment loading errors. This is the best observability pattern to know if the .env.json or the .env define |
You're right. I added the try-catch to avoid duplicate errors in tests, Fixed - try-catch only checks if env is already loaded, but |
|
Ah!ok, you can reach out the env config into tests/Config for getting more information. |
There was a problem hiding this comment.
Pull request overview
Adds first-class PHP 8 attribute-based routing to the Bow router, with unit/integration tests and a few small robustness fixes in unrelated areas (env/console/tests).
Changes:
- Introduces
Bow\Router\Attributes\*(e.g.,#[Controller],#[Get],#[Post], etc.) and anAttributeRouteRegistrarto discover/register routes via reflection. - Adds
Router::register()for registering one or many controller classes via the attribute registrar. - Improves a few edge cases (env loading guard, console raw-command lookup, test command stub directory creation) and adds docs/ignores.
Reviewed changes
Copilot reviewed 20 out of 21 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Router/Attributes/Controller.php | Adds controller-level metadata attribute (prefix/middleware/name). |
| src/Router/Attributes/Route.php | Base route attribute (path/methods/middleware/where/name). |
| src/Router/Attributes/Get.php | GET route attribute wrapper. |
| src/Router/Attributes/Post.php | POST route attribute wrapper. |
| src/Router/Attributes/Put.php | PUT route attribute wrapper. |
| src/Router/Attributes/Patch.php | PATCH route attribute wrapper. |
| src/Router/Attributes/Delete.php | DELETE route attribute wrapper. |
| src/Router/Attributes/Options.php | OPTIONS route attribute wrapper. |
| src/Router/AttributeRouteRegistrar.php | Reflection-based controller scanning and route registration. |
| src/Router/Router.php | Adds register() entry point for attribute-based registration. |
| tests/Routing/Stubs/UserControllerStub.php | Attribute-decorated controller stub for registration tests. |
| tests/Routing/Stubs/SimpleControllerStub.php | Stub demonstrating routes without #[Controller]. |
| tests/Routing/AttributeRouteTest.php | Unit tests for attribute objects + reflection behavior. |
| tests/Routing/AttributeRouteIntegrationTest.php | Integration tests for registering attributes into the router. |
| src/Support/helpers.php | Makes app_env() tolerate env not being loaded (but return type needs correction). |
| src/Configuration/EnvConfiguration.php | Avoids re-loading env if already loaded; uses config base path for .env.json. |
| src/Console/Console.php | Guards array_key_exists() against null raw command. |
| tests/Console/Stubs/CustomCommand.php | Ensures test output directory exists before writing. |
| src/Database/Barry/Model.php | Normalizes double cast handling to (float). |
| ROADMAP.md | Adds project roadmap document. |
| .gitignore | Ignores IDE folders (.vscode, .idea). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Add support for defining routes using PHP 8 attributes on controller classes.
Controllers can use #[Controller] attribute with prefix and middleware, and methods
can use #[Get], #[Post], #[Put], #[Delete], #[Patch] attributes to define routes.
The Router::register() method automatically discovers and registers routes from
controller classes. Includes 22 tests.