Remove restrictions from prependMiddleware() and middleware() methods#279
Remove restrictions from prependMiddleware() and middleware() methods#279klsoft-web wants to merge 5 commits intoyiisoft:masterfrom
Conversation
klsoft-web
commented
Feb 6, 2026
| Q | A |
|---|---|
| Is bugfix? | ❌ |
| New feature? | ❌ |
| Breaks BC? | ❌ |
| Fixed issues | #277 |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #279 +/- ##
=============================================
- Coverage 97.40% 72.72% -24.68%
+ Complexity 181 178 -3
=============================================
Files 16 16
Lines 577 550 -27
=============================================
- Hits 562 400 -162
- Misses 15 150 +135 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR removes ordering restrictions around adding middleware in Route and Group, aligning behavior with issue #277 so middleware can be added both before and after actions/routes.
Changes:
- Allow
Route::middleware()to be called afteraction()by inserting middleware before the action handler. - Allow
Route::prependMiddleware()to be called beforeaction()(always prepends). - Remove
Groupordering restrictions so middleware can be added before/afterroutes()and vice versa, and update tests/changelog accordingly.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
src/Route.php |
Adjusts middleware() behavior to insert before action when action is present; removes prependMiddleware() restriction. |
src/Group.php |
Removes state flags/exceptions that enforced ordering between routes() and middleware methods. |
tests/RouteTest.php |
Replaces exception-based tests with assertions covering new Route middleware ordering behavior. |
tests/GroupTest.php |
Updates tests to reflect relaxed ordering constraints in Group. |
CHANGELOG.md |
Documents enhancement #277. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
tests/GroupTest.php
Outdated
| @@ -439,10 +433,12 @@ public function testWithCorsWithNestedGroups2(): void | |||
| public function testMiddlewareAfterRoutes(): void | |||
| { | |||
| $group = Group::create()->routes(Route::get('/info')->action(static fn() => 'info')); | |||
There was a problem hiding this comment.
Add middleware before action to check the order of middlewares.