Skip to content

feat(router): add php 8 attributes support for route definition#349

Merged
papac merged 5 commits into
bowphp:5.xfrom
gessyken:5.x
May 20, 2026
Merged

feat(router): add php 8 attributes support for route definition#349
papac merged 5 commits into
bowphp:5.xfrom
gessyken:5.x

Conversation

@gessyken
Copy link
Copy Markdown
Contributor

@gessyken gessyken commented Jan 2, 2026

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.

@papac
Copy link
Copy Markdown
Member

papac commented Jan 3, 2026

Why did you avoid environment loading errors. This is the best observability pattern to know if the .env.json or the .env define

@gessyken
Copy link
Copy Markdown
Contributor Author

gessyken commented Jan 3, 2026

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,
but observability is more important

Fixed - try-catch only checks if env is already loaded, but
Env::configure() exceptions will propagate if .env.json is missing

@papac
Copy link
Copy Markdown
Member

papac commented Jan 3, 2026

Ah!ok, you can reach out the env config into tests/Config for getting more information.

Comment thread src/Router/Router.php
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 an AttributeRouteRegistrar to 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.

Comment thread src/Router/AttributeRouteRegistrar.php
Comment thread src/Router/AttributeRouteRegistrar.php
Comment thread src/Router/AttributeRouteRegistrar.php
Comment thread tests/Routing/AttributeRouteIntegrationTest.php
Comment thread src/Support/helpers.php
@papac papac merged commit 823b445 into bowphp:5.x May 20, 2026
0 of 8 checks passed
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.

3 participants