mirror of
https://github.com/archtechx/tenancy.git
synced 2026-05-06 19:44:03 +00:00
Handle unnamed routes
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.
This commit is contained in:
parent
dc2be5b59f
commit
bff74c8c31
2 changed files with 31 additions and 8 deletions
|
|
@ -125,7 +125,7 @@ class TenancyUrlGenerator extends UrlGenerator
|
||||||
throw new InvalidArgumentException('Attribute [name] expects a string backed enum.');
|
throw new InvalidArgumentException('Attribute [name] expects a string backed enum.');
|
||||||
}
|
}
|
||||||
|
|
||||||
[$name] = $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);
|
return parent::route($name, $parameters, $absolute);
|
||||||
}
|
}
|
||||||
|
|
@ -140,12 +140,10 @@ class TenancyUrlGenerator extends UrlGenerator
|
||||||
{
|
{
|
||||||
$name = $route->getName();
|
$name = $route->getName();
|
||||||
|
|
||||||
if ($name) {
|
[$prefixedName, $parameters] = $this->prepareRouteInputs(Arr::wrap($parameters), $name);
|
||||||
[$prefixedName, $parameters] = $this->prepareRouteInputs($name, Arr::wrap($parameters));
|
|
||||||
|
|
||||||
if ($prefixedName !== $name && $tenantRoute = $this->routes->getByName($prefixedName)) {
|
if ($name && $prefixedName !== $name && $tenantRoute = $this->routes->getByName($prefixedName)) {
|
||||||
$route = $tenantRoute;
|
$route = $tenantRoute;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return parent::toRoute($route, $parameters, $absolute);
|
return parent::toRoute($route, $parameters, $absolute);
|
||||||
|
|
@ -170,10 +168,13 @@ class TenancyUrlGenerator extends UrlGenerator
|
||||||
* To skip these modifications, pass the bypass parameter in route parameters.
|
* To skip these modifications, pass the bypass parameter in route parameters.
|
||||||
* Before returning the modified route inputs, the bypass parameter is removed from the parameters.
|
* Before returning the modified route inputs, the bypass parameter is removed from the parameters.
|
||||||
*/
|
*/
|
||||||
protected function prepareRouteInputs(string $name, array $parameters): array
|
protected function prepareRouteInputs(array $parameters, string|null $name): array
|
||||||
{
|
{
|
||||||
if (! $this->routeBehaviorModificationBypassed($parameters)) {
|
if (! $this->routeBehaviorModificationBypassed($parameters)) {
|
||||||
$name = $this->routeNameOverride($name) ?? $this->prefixRouteName($name);
|
if (! is_null($name)) {
|
||||||
|
$name = $this->routeNameOverride($name) ?? $this->prefixRouteName($name);
|
||||||
|
}
|
||||||
|
|
||||||
$parameters = $this->addTenantParameter($parameters);
|
$parameters = $this->addTenantParameter($parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -423,3 +423,25 @@ test('the toRoute method can automatically prefix the passed route name', functi
|
||||||
// Passing the bypass parameter skips the name prefixing, so the method returns the central route URL
|
// 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');
|
expect(url()->toRoute($centralRoute, ['central' => true], true))->toBe('http://localhost/central/home');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('toRoute modifies parameters even when the route has no name', function () {
|
||||||
|
config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]);
|
||||||
|
|
||||||
|
TenancyUrlGenerator::$passTenantParameterToRoutes = true;
|
||||||
|
|
||||||
|
$unnamedRoute = Route::get('/unnamed', fn () => 'unnamed');
|
||||||
|
|
||||||
|
$tenant = Tenant::create();
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
|
// The tenant parameter is added to the URL even for unnamed routes
|
||||||
|
expect(url()->toRoute($unnamedRoute, [], true))
|
||||||
|
->toBe("http://localhost/unnamed?tenant={$tenant->getTenantKey()}");
|
||||||
|
|
||||||
|
// The bypass parameter prevents passing the tenant parameter and is stripped from the URL
|
||||||
|
expect(url()->toRoute($unnamedRoute, ['central' => true], true))
|
||||||
|
->toBe("http://localhost/unnamed")
|
||||||
|
->not()->toContain('tenant=')
|
||||||
|
->not()->toContain('central=');
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue