mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 15:14:03 +00:00
Change URI prefix order during route cloning (#49)
* Change URI prefix order [ci skip] * Move route cloning to `$this->app->booted()` [ci skip] * Delete note about LW v2 * Improve comments/docblock [skip ci] * Fix comment in test, improve assertion [skip ci]
This commit is contained in:
parent
7317d2638a
commit
b789f5c561
3 changed files with 53 additions and 47 deletions
|
|
@ -20,8 +20,6 @@ use Illuminate\Support\Facades\Route as RouteFacade;
|
||||||
use Stancl\Tenancy\Middleware\InitializeTenancyByPath;
|
use Stancl\Tenancy\Middleware\InitializeTenancyByPath;
|
||||||
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
|
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
|
||||||
|
|
||||||
// todo update all the docblock sections here, including the Livewire references
|
|
||||||
|
|
||||||
class TenancyServiceProvider extends ServiceProvider
|
class TenancyServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
// By default, no namespace is used to support the callable array syntax.
|
// By default, no namespace is used to support the callable array syntax.
|
||||||
|
|
@ -140,21 +138,24 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
|
|
||||||
protected function overrideUrlInTenantContext(): void
|
protected function overrideUrlInTenantContext(): void
|
||||||
{
|
{
|
||||||
// Import your tenant model!
|
/**
|
||||||
// \Stancl\Tenancy\Bootstrappers\RootUrlBootstrapper::$rootUrlOverride = function (Tenant $tenant, string $originalRootUrl) {
|
* Import your tenant model!
|
||||||
// $tenantDomain = $tenant instanceof \Stancl\Tenancy\Contracts\SingleDomainTenant
|
*
|
||||||
// ? $tenant->domain
|
* \Stancl\Tenancy\Bootstrappers\RootUrlBootstrapper::$rootUrlOverride = function (Tenant $tenant, string $originalRootUrl) {
|
||||||
// : $tenant->domains->first()->domain;
|
* $tenantDomain = $tenant instanceof \Stancl\Tenancy\Contracts\SingleDomainTenant
|
||||||
//
|
* ? $tenant->domain
|
||||||
// $scheme = str($originalRootUrl)->before('://');
|
* : $tenant->domains->first()->domain;
|
||||||
//
|
*
|
||||||
// // If you're using subdomain identification:
|
* $scheme = str($originalRootUrl)->before('://');
|
||||||
// // $originalDomain = str($originalRootUrl)->after($scheme . '://');
|
*
|
||||||
// // return $scheme . '://' . $tenantDomain . '.' . $originalDomain . '/';
|
* // If you're using subdomain identification:
|
||||||
//
|
* // $originalDomain = str($originalRootUrl)->after($scheme . '://');
|
||||||
// // If you're using domain identification:
|
* // return $scheme . '://' . $tenantDomain . '.' . $originalDomain . '/';
|
||||||
// return $scheme . '://' . $tenantDomain . '/';
|
*
|
||||||
// };
|
* // If you're using domain identification:
|
||||||
|
* return $scheme . '://' . $tenantDomain . '/';
|
||||||
|
* };
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
public function register()
|
public function register()
|
||||||
|
|
@ -199,32 +200,6 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
|
|
||||||
if (tenancy()->globalStackHasMiddleware(config('tenancy.identification.path_identification_middleware'))) {
|
if (tenancy()->globalStackHasMiddleware(config('tenancy.identification.path_identification_middleware'))) {
|
||||||
TenancyUrlGenerator::$prefixRouteNames = true;
|
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')
|
->middleware('tenant')
|
||||||
->group(base_path('routes/tenant.php'));
|
->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()
|
protected function makeTenancyMiddlewareHighestPriority()
|
||||||
{
|
{
|
||||||
// PreventAccessFromUnwantedDomains has even higher priority than the identification middleware
|
// PreventAccessFromUnwantedDomains has even higher priority than the identification middleware
|
||||||
|
|
|
||||||
|
|
@ -174,7 +174,7 @@ class CloneRoutesAsTenant
|
||||||
return $action
|
return $action
|
||||||
->put('as', $newRouteNamePrefix)
|
->put('as', $newRouteNamePrefix)
|
||||||
->put('middleware', $newRouteMiddleware)
|
->put('middleware', $newRouteMiddleware)
|
||||||
->put('prefix', '/{' . PathTenantResolver::tenantParameterName() . '}/' . $route->getPrefix());
|
->put('prefix', $route->getPrefix() . '/{' . PathTenantResolver::tenantParameterName() . '}');
|
||||||
})->toArray();
|
})->toArray();
|
||||||
|
|
||||||
/** @var Route $newRoute */
|
/** @var Route $newRoute */
|
||||||
|
|
|
||||||
|
|
@ -258,11 +258,13 @@ test('the clone action prefixes already prefixed routes correctly', function ()
|
||||||
$clonedRouteUrl = route($clonedRouteName, ['tenant' => $tenant = Tenant::create()]);
|
$clonedRouteUrl = route($clonedRouteName, ['tenant' => $tenant = Tenant::create()]);
|
||||||
|
|
||||||
// The cloned route is prefixed correctly
|
// The cloned route is prefixed correctly
|
||||||
expect($clonedRoute->getPrefix())->toBe('{tenant}/' . $prefix);
|
expect($clonedRoute->getPrefix())->toBe("{$prefix}/{tenant}");
|
||||||
|
|
||||||
expect($clonedRouteUrl)
|
expect($clonedRouteUrl)
|
||||||
->toContain('/' . $tenant->getTenantKey() . '/' . $prefix . '/home')
|
// Original prefix does not occur in the cloned route's URL
|
||||||
->not()->toContain($prefix . '/' . $tenant->getTenantKey() . '/' . $prefix . '/home');
|
->not()->toContain("/{$prefix}/{$tenant->getTenantKey()}/{$prefix}")
|
||||||
|
// Route is prefixed correctly
|
||||||
|
->toBe("http://localhost/{$prefix}/{$tenant->getTenantKey()}/home");
|
||||||
|
|
||||||
// The cloned route is accessible
|
// The cloned route is accessible
|
||||||
pest()->get($clonedRouteUrl)->assertSee('Tenancy initialized.');
|
pest()->get($clonedRouteUrl)->assertSee('Tenancy initialized.');
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue