mirror of
https://github.com/archtechx/tenancy.git
synced 2026-03-21 23:44:03 +00:00
Add toRoute() override to TenancyUrlGenerator
Also update `route()` override since `parent::route()` calls `toRoute()` under the hood (similarly to how `parent::temporarySignedRoute()` calls `route()`)
This commit is contained in:
parent
a306f1e972
commit
3103ff60c0
2 changed files with 50 additions and 1 deletions
|
|
@ -114,7 +114,15 @@ class TenancyUrlGenerator extends UrlGenerator
|
|||
throw new InvalidArgumentException('Attribute [name] expects a string backed enum.');
|
||||
}
|
||||
|
||||
[$name, $parameters] = $this->prepareRouteInputs($name, Arr::wrap($parameters)); // @phpstan-ignore argument.type
|
||||
$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];
|
||||
}
|
||||
|
||||
return parent::route($name, $parameters, $absolute);
|
||||
}
|
||||
|
|
@ -142,6 +150,25 @@ class TenancyUrlGenerator extends UrlGenerator
|
|||
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.
|
||||
*/
|
||||
public function toRoute($route, $parameters, $absolute)
|
||||
{
|
||||
$name = $route->getName();
|
||||
|
||||
if ($name) {
|
||||
[$prefixedName, $parameters] = $this->prepareRouteInputs($name, Arr::wrap($parameters)); // @phpstan-ignore argument.type
|
||||
|
||||
if ($prefixedName !== $name && $tenantRoute = $this->routes->getByName($prefixedName)) {
|
||||
$route = $tenantRoute;
|
||||
}
|
||||
}
|
||||
|
||||
return parent::toRoute($route, $parameters, $absolute);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return bool indicating if the bypass parameter was in $parameters.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -401,3 +401,25 @@ test('the bypass parameter works correctly with temporarySignedRoute', function(
|
|||
->toContain('localhost/foo')
|
||||
->not()->toContain('central='); // Bypass parameter gets removed from the generated URL
|
||||
});
|
||||
|
||||
test('the toRoute method can automatically prefix the passed route name', function () {
|
||||
config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]);
|
||||
|
||||
Route::get('/central/home', fn () => 'central')->name('home');
|
||||
Route::get('/tenant/home', fn () => 'tenant')->name('tenant.home');
|
||||
|
||||
TenancyUrlGenerator::$prefixRouteNames = true;
|
||||
|
||||
$tenant = Tenant::create();
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
$centralRoute = Route::getRoutes()->getByName('home');
|
||||
|
||||
// url()->toRoute() prefixes the name of the passed route ('home') with the tenant prefix
|
||||
// and generates the URL for the tenant route (as if the 'tenant.home' route was passed to the method)
|
||||
expect(url()->toRoute($centralRoute, [], true))->toBe('http://localhost/tenant/home');
|
||||
|
||||
// Passing the bypass parameter skips the name prefixing, so the method returns the central route URL
|
||||
expect(url()->toRoute($centralRoute, ['central' => true], true))->toBe('http://localhost/central/home');
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue