[4.x] TenancyUrlGenerator: override toRoute(), refactor#1439
Conversation
Also update `route()` override since `parent::route()` calls `toRoute()` under the hood (similarly to how `parent::temporarySignedRoute()` calls `route()`)
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1439 +/- ##
============================================
+ Coverage 85.96% 85.98% +0.02%
- Complexity 1174 1175 +1
============================================
Files 185 185
Lines 3426 3425 -1
============================================
Hits 2945 2945
+ Misses 481 480 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
The `toRoute()` and `route()` overrides cover `temporarySignedRoute())`, so we don't need to specifically override `temporarySIgnedRoute()` anymore. `route()` override got simplified since everything except for the name prefixing is delegated to the lower-level `toRoute()` method.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughCentralized route-name prefixing and tenant-parameter handling by introducing a ChangesTenancyUrlGenerator updates
Sequence Diagram(s)sequenceDiagram
participant Caller
participant TenancyUrlGenerator
participant Router
participant ParentUrlGenerator
Caller->>TenancyUrlGenerator: call route()/toRoute($route, $parameters, $absolute)
TenancyUrlGenerator->>Router: lookup prefixed route name (if computed)
Router-->>TenancyUrlGenerator: return prefixed route or null
TenancyUrlGenerator->>ParentUrlGenerator: parent::toRoute(resolvedRoute, mutatedParameters, $absolute)
ParentUrlGenerator-->>Caller: generated URL
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/Overrides/TenancyUrlGenerator.php`:
- Around line 141-149: The parameter mutation is skipped for unnamed Route
objects because $route->getName() gates the call to prepareRouteInputs; change
the logic in TenancyUrlGenerator::toRoute so you always call
$this->prepareRouteInputs($name, Arr::wrap($parameters)) (or call it with
null/$name when route has no name) to apply tenant-parameter injection and
bypass-parameter removal, but keep the route swap conditional: only replace
$route when $prefixedName !== $name and $this->routes->getByName($prefixedName)
returns a route; ensure prepareRouteInputs and the passTenantParameterToRoutes
behavior run even when $route->getName() is falsy before delegating to
parent::toRoute().
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 17d8031d-e2be-43de-9b4c-44e8b71136d2
📒 Files selected for processing (3)
src/Bootstrappers/UrlGeneratorBootstrapper.phpsrc/Overrides/TenancyUrlGenerator.phptests/Bootstrappers/UrlGeneratorBootstrapperTest.php
If an unnamed route is passed to url()->toRoute(), the generated URL will receive the tenant parameter as long as the bypass parameter wasn't passed. Also remove the bypass parameter from the generated URL.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/Overrides/TenancyUrlGenerator.php`:
- Around line 128-130: The route() method currently calls prepareRouteInputs()
and discards the mutated $parameters, causing duplicate work and potential side
effects; create a small helper (e.g., normalizeRouteName or prepareRouteName) in
the TenancyUrlGenerator class that performs only the name normalization logic
extracted from prepareRouteInputs() and returns the normalized $name without
mutating $parameters, then update route() to call that helper and pass the
original $parameters into parent::route($name, $parameters, $absolute); keep
prepareRouteInputs() and toRoute() unchanged so full parameter resolution still
happens only when needed.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 1ef2ed10-fc71-412c-80f0-c96ee9ac799c
📒 Files selected for processing (2)
src/Overrides/TenancyUrlGenerator.phptests/Bootstrappers/UrlGeneratorBootstrapperTest.php
|
@coderabbitai full review |
✅ Actions performedFull review triggered. |
There was a problem hiding this comment.
Pull request overview
This PR extends the tenancy-aware URL generation behavior by overriding TenancyUrlGenerator::toRoute() so that tenant route-name prefixing and tenant-parameter injection also apply to URL generation flows that bypass the route() helper (notably Livewire v4’s update URI generation).
Changes:
- Override
TenancyUrlGenerator::toRoute()to swap a passed route for its tenant-prefixed equivalent (when applicable) and to apply tenant-parameter injection/bypass behavior. - Simplify the
route()override to focus on ensuring the lookup name is prefixed beforegetByName()runs, delegating parameter handling totoRoute(). - Add/extend tests to cover
toRoute()name-prefixing, tenant-parameter injection for unnamed routes, and bypass behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/Overrides/TenancyUrlGenerator.php |
Adds toRoute() override and refactors route-name prefixing/parameter handling flow. |
tests/Bootstrappers/UrlGeneratorBootstrapperTest.php |
Adds test coverage for toRoute() prefixing, unnamed-route parameter injection, and bypass behavior. |
src/Bootstrappers/UrlGeneratorBootstrapper.php |
Updates documentation comments to reflect the broadened “affected methods” behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| [$name, $parameters] = $this->prepareRouteInputs($name, Arr::wrap($parameters)); // @phpstan-ignore argument.type | ||
| [$name] = $this->prepareRouteInputs(Arr::wrap($parameters), $name); // @phpstan-ignore argument.type | ||
|
|
||
| return parent::route($name, $parameters, $absolute); |
There was a problem hiding this comment.
This might be a reasonable suggestion but to do the name prefixing (what we do in prepareRouteInputs) we do need the parameters array so looking at that method, the only overhead we'd avoid would be the $parameter manipulation as mentioned here:
$parameters = $this->addTenantParameter($parameters);
// ...
// Remove bypass parameter from the route parameters
unset($parameters[static::$bypassParameter]);Which isn't nothing but probably not worth complicating the code over. I'm just noting this here to acknowledge this suggestion in case we decide to change this in the future.
toRoute()toRoute(), refactor
This PR adds the
toRoute()method override toTenancyUrlGenerator.toRoute()now attempts to find a tenant equivalent of the passed route (= a route with the same name as the passed one, but with the tenant prefix) and generates URL for the tenant route. This behavior can be bypassed using the bypass parameter, like with theroute()method overrideTenancyUrlGeneratorhad until now.The primary reason for adding this is that Livewire v4 no longer uses the
route()helper (which automatically prefixes the passed route name because of the override inTenancyUrlGenerator) inLivewire::getUpdateUri(). Now, it usestoRoute()(livewire/livewire@544aa3d#diff-e7609f8b0a60bde5a85067803d4e2f08f235c7cee9225a51ea67a85ff9a1d694R52), which didn't automatically swap the route for its 'tenant.'-prefixed equivalent in tenant context (until now). So for the Livewire integration to work with path identification, we need to overridetoRoute()as described.The
temporarySignedRoute()override got removed becausetemporarySignedRoute()callsroute()under the hood, there's no need to specifically overridetemporarySignedRoute().The
route()override got simplified. Sinceroute()usestoRoute()under the hood, theroute()override only has to have the prefixing logic. The rest is delegated totoRoute().Comments in TenancyUrlGenerator and UrlGeneratorBootstrapper got updated to be more accurate. All intentionally affected methods are listed in TenancyUrlGenerator's docblock.
Summary by CodeRabbit
Improvements
Documentation
Tests