1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 15:34:04 +00:00

Improve TenancyUrlGenerator and RootUrlBootstrapperTest clarity

This commit is contained in:
lukinovec 2024-12-23 15:08:41 +01:00
parent 5c8933bd14
commit 81917b86fb
2 changed files with 48 additions and 26 deletions

View file

@ -62,18 +62,31 @@ class TenancyUrlGenerator extends UrlGenerator
$url = parent::route($name, $parameters, $absolute); $url = parent::route($name, $parameters, $absolute);
if (isset($parameters[PathTenantResolver::tenantParameterName()])) { if (isset($parameters[PathTenantResolver::tenantParameterName()])) {
// Ensure the tenant key is present in the URL just once /**
// This is necessary when using UrlGeneratorBootstrapper with RootUrlBootstrapper * Ensure the tenant key appears in the final URL only once.
$tenantId = $parameters[PathTenantResolver::tenantParameterName()]; * This adjustment is necessary when RootUrlBootstrapper is enabled (and used as intended).
$afterTenant = str($url)->afterLast($tenantId)->toString(); *
$beforeTenant = str($url)->before($tenantId)->toString(); * When RootUrlBootstrapper adds the tenant key to the root URL:
* - The root URL includes the tenant key (http://localhost/tenantfoo).
* - Passing the tenant key as a parameter to `parent::route()` adds it again, causing duplication.
*
* To fix this:
* - For relative URLs: Include only the part AFTER the tenant key.
* - For absolute URLs: Rebuild the URL so that the tenant key is included exactly once.
*/
$tenantKey = $parameters[PathTenantResolver::tenantParameterName()];
if (! $absolute && str(url('/'))->contains($tenantId)) { // Separate the URL into parts before the first and after the last tenant key
// If the URL should be relative and the tenant key is already present in the full URL, don't add it again $urlBeforeTenantKey = str($url)->before($tenantKey)->toString(); // e.g. "http://localhost/"
return $afterTenant; $urlAfterTenantKey = str($url)->afterLast($tenantKey)->toString(); // e.g. "/home"
if (! $absolute && str(url('/'))->contains($tenantKey)) {
// For relative URLs, return only the part after the tenant key
return $urlAfterTenantKey;
} }
return $beforeTenant . $tenantId . $afterTenant; // Reconstruct the URL with the tenant key appearing exactly once
return $urlBeforeTenantKey . $tenantKey . $urlAfterTenantKey;
} }
return $url; return $url;

View file

@ -72,31 +72,40 @@ test('root url bootstrapper overrides the root url when tenancy gets initialized
}); });
test('root url bootstrapper can be used with url generator bootstrapper', function() { test('root url bootstrapper can be used with url generator bootstrapper', function() {
/**
* Order matters when combining these two bootstrappers.
* Before overriding the URL generator's root URL, we need to bind TenancyUrlGenerator.
* Otherwise (when using RootUrlBootstrapper BEFORE UrlGeneratorBootstrapper),
* the original URL generator's root URL will be changed, and only after that will the TenancyUrlGenerator bound,
* ultimately making the root URL override pointless.
*/
config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class, RootUrlBootstrapper::class]]); config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class, RootUrlBootstrapper::class]]);
TenancyUrlGenerator::$prefixRouteNames = true; TenancyUrlGenerator::$prefixRouteNames = true;
TenancyUrlGenerator::$passTenantParameterToRoutes = true; TenancyUrlGenerator::$passTenantParameterToRoutes = true;
RootUrlBootstrapper::$rootUrlOverride = fn (Tenant $tenant, string $originalRootUrl) => $originalRootUrl . '/' . $tenant->getTenantKey();
Route::get('/', function () { Route::get('/home', fn () => 'home')->name('home');
return true; Route::get('/{tenant}/home', fn () => 'tenant.home')->name('tenant.home')->middleware(InitializeTenancyByPath::class);
})->name('home');
Route::get('/{tenant}', function () { expect(url('/home'))->toBe('http://localhost/home');
return true;
})->name('tenant.home')->middleware(InitializeTenancyByPath::class);
$rootUrlOverride = function (Tenant $tenant) { expect(route('home'))->toBe('http://localhost/home');
return 'http://localhost/' . $tenant->getTenantKey(); expect(route('home', absolute: false))->toBe('/home');
};
$tenant = Tenant::create(['id' => 'acme']); tenancy()->initialize(Tenant::create(['id' => 'acme']));
RootUrlBootstrapper::$rootUrlOverride = $rootUrlOverride; // The url() helper should generate the full URL containing the tenant key
expect(url('/home'))->toBe('http://localhost/acme/home');
expect(route('home'))->toBe('http://localhost'); /**
* The absolute path should return the correct absolute path, containing just one tenant key,
tenancy()->initialize($tenant); * and the relative path should still be /home.
*
expect(route('home'))->toBe('http://localhost/acme'); * We use string manipulation in the route() method override for this to behave correctly.
expect(url('/'))->toBe('http://localhost/acme'); *
* @see TenancyUrlGenerator
*/
expect(route('home'))->toBe('http://localhost/acme/home');
expect(route('home', absolute: false))->toBe('/home');
}); });