diff --git a/assets/TenancyServiceProvider.stub.php b/assets/TenancyServiceProvider.stub.php index 006cb190..3f89243f 100644 --- a/assets/TenancyServiceProvider.stub.php +++ b/assets/TenancyServiceProvider.stub.php @@ -20,8 +20,6 @@ use Illuminate\Support\Facades\Route as RouteFacade; use Stancl\Tenancy\Middleware\InitializeTenancyByPath; use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData; -// todo update all the docblock sections here, including the Livewire references - class TenancyServiceProvider extends ServiceProvider { // By default, no namespace is used to support the callable array syntax. @@ -140,21 +138,24 @@ class TenancyServiceProvider extends ServiceProvider protected function overrideUrlInTenantContext(): void { - // Import your tenant model! - // \Stancl\Tenancy\Bootstrappers\RootUrlBootstrapper::$rootUrlOverride = function (Tenant $tenant, string $originalRootUrl) { - // $tenantDomain = $tenant instanceof \Stancl\Tenancy\Contracts\SingleDomainTenant - // ? $tenant->domain - // : $tenant->domains->first()->domain; - // - // $scheme = str($originalRootUrl)->before('://'); - // - // // If you're using subdomain identification: - // // $originalDomain = str($originalRootUrl)->after($scheme . '://'); - // // return $scheme . '://' . $tenantDomain . '.' . $originalDomain . '/'; - // - // // If you're using domain identification: - // return $scheme . '://' . $tenantDomain . '/'; - // }; + /** + * Import your tenant model! + * + * \Stancl\Tenancy\Bootstrappers\RootUrlBootstrapper::$rootUrlOverride = function (Tenant $tenant, string $originalRootUrl) { + * $tenantDomain = $tenant instanceof \Stancl\Tenancy\Contracts\SingleDomainTenant + * ? $tenant->domain + * : $tenant->domains->first()->domain; + * + * $scheme = str($originalRootUrl)->before('://'); + * + * // If you're using subdomain identification: + * // $originalDomain = str($originalRootUrl)->after($scheme . '://'); + * // return $scheme . '://' . $tenantDomain . '.' . $originalDomain . '/'; + * + * // If you're using domain identification: + * return $scheme . '://' . $tenantDomain . '/'; + * }; + */ } public function register() @@ -199,32 +200,6 @@ class TenancyServiceProvider extends ServiceProvider if (tenancy()->globalStackHasMiddleware(config('tenancy.identification.path_identification_middleware'))) { TenancyUrlGenerator::$prefixRouteNames = true; - - /** @var CloneRoutesAsTenant $cloneRoutes */ - $cloneRoutes = app(CloneRoutesAsTenant::class); - - /** - * You can provide a closure for cloning a specific route, e.g.: - * $cloneRoutes->cloneUsing('welcome', function () { - * RouteFacade::get('/tenant-welcome', fn () => 'Current tenant: ' . tenant()->getTenantKey()) - * ->middleware(['universal', InitializeTenancyByPath::class]) - * ->name('tenant.welcome'); - * }); - * - * To make Livewire v2 (2.12.2+) work with kernel path identification, - * use this closure to override the livewire.message-localized route: - * - * $cloneRoutes->cloneUsing('livewire.message-localized', function (Route $route) { - * $route->setUri(str($route->uri())->replaceFirst('locale', $tenantParameter = PathTenantResolver::tenantParameterName())); - * $route->parameterNames[0] = $tenantParameter; - * $route->middleware('tenant'); - * }); - * - * To see the default behavior of cloning the universal routes, check out the cloneRoute() method in CloneRoutesAsTenant. - * @see CloneRoutesAsTenant - */ - - $cloneRoutes->handle(); } } @@ -249,9 +224,38 @@ class TenancyServiceProvider extends ServiceProvider ->middleware('tenant') ->group(base_path('routes/tenant.php')); } + + // Delete this condition when using route-level path identification + if (tenancy()->globalStackHasMiddleware(config('tenancy.identification.path_identification_middleware'))) { + $this->cloneRoutes(); + } }); } + /** + * Clone universal routes as tenant. + * + * @see CloneRoutesAsTenant + */ + protected function cloneRoutes(): void + { + /** @var CloneRoutesAsTenant $cloneRoutes */ + $cloneRoutes = $this->app->make(CloneRoutesAsTenant::class); + + /** + * You can provide a closure for cloning a specific route, e.g.: + * $cloneRoutes->cloneUsing('welcome', function () { + * RouteFacade::get('/tenant-welcome', fn () => 'Current tenant: ' . tenant()->getTenantKey()) + * ->middleware(['universal', InitializeTenancyByPath::class]) + * ->name('tenant.welcome'); + * }); + * + * To see the default behavior of cloning the universal routes, check out the cloneRoute() method in CloneRoutesAsTenant. + */ + + $cloneRoutes->handle(); + } + protected function makeTenancyMiddlewareHighestPriority() { // PreventAccessFromUnwantedDomains has even higher priority than the identification middleware diff --git a/src/Actions/CloneRoutesAsTenant.php b/src/Actions/CloneRoutesAsTenant.php index 10069e19..fdd2a822 100644 --- a/src/Actions/CloneRoutesAsTenant.php +++ b/src/Actions/CloneRoutesAsTenant.php @@ -174,7 +174,7 @@ class CloneRoutesAsTenant return $action ->put('as', $newRouteNamePrefix) ->put('middleware', $newRouteMiddleware) - ->put('prefix', '/{' . PathTenantResolver::tenantParameterName() . '}/' . $route->getPrefix()); + ->put('prefix', $route->getPrefix() . '/{' . PathTenantResolver::tenantParameterName() . '}'); })->toArray(); /** @var Route $newRoute */ diff --git a/tests/CloneActionTest.php b/tests/CloneActionTest.php index 42ce2158..8e08ef32 100644 --- a/tests/CloneActionTest.php +++ b/tests/CloneActionTest.php @@ -258,11 +258,13 @@ test('the clone action prefixes already prefixed routes correctly', function () $clonedRouteUrl = route($clonedRouteName, ['tenant' => $tenant = Tenant::create()]); // The cloned route is prefixed correctly - expect($clonedRoute->getPrefix())->toBe('{tenant}/' . $prefix); + expect($clonedRoute->getPrefix())->toBe("{$prefix}/{tenant}"); expect($clonedRouteUrl) - ->toContain('/' . $tenant->getTenantKey() . '/' . $prefix . '/home') - ->not()->toContain($prefix . '/' . $tenant->getTenantKey() . '/' . $prefix . '/home'); + // Original prefix does not occur in the cloned route's URL + ->not()->toContain("/{$prefix}/{$tenant->getTenantKey()}/{$prefix}") + // Route is prefixed correctly + ->toBe("http://localhost/{$prefix}/{$tenant->getTenantKey()}/home"); // The cloned route is accessible pest()->get($clonedRouteUrl)->assertSee('Tenancy initialized.');