From ff68edfe5db30f08611e414747fb50609ee7b715 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Tue, 11 Feb 2025 12:10:20 +0100 Subject: [PATCH] Add `$override` functionality to TenancyUrlGenerator --- src/Overrides/TenancyUrlGenerator.php | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Overrides/TenancyUrlGenerator.php b/src/Overrides/TenancyUrlGenerator.php index a77f9b88..c7e4a862 100644 --- a/src/Overrides/TenancyUrlGenerator.php +++ b/src/Overrides/TenancyUrlGenerator.php @@ -37,8 +37,10 @@ class TenancyUrlGenerator extends UrlGenerator * - route('home', [$bypassParameter => true]) => app.test/ * - route('tenant.home', [$bypassParameter => true]) => app.test/tenant -- query string identification (no query string passed) * - * With path identification, the tenant parameter is passed automatically by URL::defaults(), so in that case, - * this only affects the automatic route name prefixing. + * With path identification, it is recommended to pass the tenant parameter automatically by setting + * UrlGeneratorBootstrapper::$addTenantParameterToDefaults to true. + * In that case, this class should only affect the automatic route name prefixing, + * and the forceful route name overrides set in the $override property. * * @see UrlGeneratorBootstrapper */ @@ -65,6 +67,21 @@ class TenancyUrlGenerator extends UrlGenerator */ public static bool $passTenantParameterToRoutes = false; + /** + * Route names that should always be overridden. + * This behavior can still be bypassed by passing the bypass parameter. + * + * For example, Jetstream integration: + * [ + * 'profile.show' => 'tenant.profile.show', + * 'two-factor.login' => 'tenant.two-factor.login', + * ] + * + * `route('profile.show')` will return an URL as if you called `route('tenant.profile.show')`. + * `route('profile.show', ['central' => true])` will return an URL as if you called `route('profile.show')`. + */ + public static array $override = []; + /** * Override the route() method so that the route name gets prefixed * and the tenant parameter gets added when in tenant context. @@ -117,7 +134,7 @@ class TenancyUrlGenerator extends UrlGenerator protected function prepareRouteInputs(string $name, array $parameters): array { if (! $this->routeBehaviorModificationBypassed($parameters)) { - $name = $this->prefixRouteName($name); + $name = $this->routeNameOverride($name) ?: $this->prefixRouteName($name); $parameters = $this->addTenantParameter($parameters); } @@ -148,4 +165,8 @@ class TenancyUrlGenerator extends UrlGenerator { return tenant() && static::$passTenantParameterToRoutes ? array_merge($parameters, [PathTenantResolver::tenantParameterName() => tenant()->getTenantKey()]) : $parameters; } + + protected function routeNameOverride(string $name) { + return static::$override[$name] ?? false; + } }