From 8345d648123547c8a9cab588bef358ec1e44ca0e Mon Sep 17 00:00:00 2001 From: lukinovec Date: Tue, 10 Mar 2026 12:17:09 +0100 Subject: [PATCH] Simplify TenancyUrlGenerator 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. --- src/Overrides/TenancyUrlGenerator.php | 45 +++++++-------------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/src/Overrides/TenancyUrlGenerator.php b/src/Overrides/TenancyUrlGenerator.php index 851f174c..b8930fe4 100644 --- a/src/Overrides/TenancyUrlGenerator.php +++ b/src/Overrides/TenancyUrlGenerator.php @@ -105,8 +105,16 @@ class TenancyUrlGenerator extends UrlGenerator public static bool $passQueryParameter = true; /** - * Override the route() method so that the route name gets prefixed - * and the tenant parameter gets added when in tenant context. + * Override the route() method to prefix the route name before $this->routes->getByName($name) is called + * in the parent route() call. + * + * This is necessary because $this->routes->getByName($name) is called to retrieve the route + * before passing it to toRoute(). If only the prefixed route (e.g. 'tenant.foo') is registered + * and the original ('foo') isn't, route() would throw a RouteNotFoundException. + * So route() has to be overridden to prefix the passed route name, even though toRoute() is overridden already. + * + * Only the name is taken from prepareRouteInputs() here — parameter handling + * (adding tenant parameter, removing bypass parameter) is delegated to toRoute(). */ public function route($name, $parameters = [], $absolute = true) { @@ -114,42 +122,11 @@ class TenancyUrlGenerator extends UrlGenerator throw new InvalidArgumentException('Attribute [name] expects a string backed enum.'); } - $wrappedParameters = Arr::wrap($parameters); - - [$name, $parameters] = $this->prepareRouteInputs($name, $wrappedParameters); // @phpstan-ignore argument.type - - if (isset($wrappedParameters[static::$bypassParameter])) { - // If the bypass parameter was passed, we need to add it back to the parameters after prepareRouteInputs() removes it, - // so that the underlying toRoute() call in parent::route() can bypass the behavior modification as well. - $parameters[static::$bypassParameter] = $wrappedParameters[static::$bypassParameter]; - } + [$name] = $this->prepareRouteInputs($name, Arr::wrap($parameters)); // @phpstan-ignore argument.type return parent::route($name, $parameters, $absolute); } - /** - * Override the temporarySignedRoute() method so that the route name gets prefixed - * and the tenant parameter gets added when in tenant context. - */ - public function temporarySignedRoute($name, $expiration, $parameters = [], $absolute = true) - { - if ($name instanceof BackedEnum && ! is_string($name = $name->value)) { - throw new InvalidArgumentException('Attribute [name] expects a string backed enum.'); - } - - $wrappedParameters = Arr::wrap($parameters); - - [$name, $parameters] = $this->prepareRouteInputs($name, $wrappedParameters); // @phpstan-ignore argument.type - - if (isset($wrappedParameters[static::$bypassParameter])) { - // If the bypass parameter was passed, we need to add it back to the parameters after prepareRouteInputs() removes it, - // so that the underlying route() call in parent::temporarySignedRoute() can bypass the behavior modification as well. - $parameters[static::$bypassParameter] = $wrappedParameters[static::$bypassParameter]; - } - - return parent::temporarySignedRoute($name, $expiration, $parameters, $absolute); - } - /** * Override the toRoute() method so that the route name gets prefixed * and the tenant parameter gets added when in tenant context.