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

set tenant as default url parameter

This commit is contained in:
Abrar Ahmad 2022-08-29 14:46:22 +05:00
parent eade69c3f4
commit 856d8b0b02
2 changed files with 27 additions and 1 deletions

View file

@ -7,6 +7,9 @@ namespace Stancl\Tenancy\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\URL;
use Stancl\Tenancy\Events\InitializingTenancy;
use Stancl\Tenancy\Exceptions\RouteIsMissingTenantParameterException;
use Stancl\Tenancy\Resolvers\PathTenantResolver;
use Stancl\Tenancy\Tenancy;
@ -37,6 +40,11 @@ class InitializeTenancyByPath extends IdentificationMiddleware
// We don't want to initialize tenancy if the tenant is
// simply injected into some route controller action.
if ($route->parameterNames()[0] === PathTenantResolver::$tenantParameterName) {
// Set tenant as a default parameter for the URLs in the current request
Event::listen(InitializingTenancy::class, function (InitializingTenancy $event) {
URL::defaults([PathTenantResolver::$tenantParameterName => $event->tenancy->tenant->getTenantKey()]);
});
return $this->initializeTenancy(
$request,
$next,

View file

@ -18,7 +18,11 @@ beforeEach(function () {
], function () {
Route::get('/foo/{a}/{b}', function ($a, $b) {
return "$a + $b";
});
})->name('foo');
Route::get('/baz/{a}/{b}', function ($a, $b) {
return "$a - $b";
})->name('baz');
});
});
@ -123,3 +127,17 @@ test('tenant parameter name can be customized', function () {
->withoutExceptionHandling()
->get('/acme/foo/abc/xyz');
});
test('tenant set as a default parameter for the URLs', function () {
Tenant::create([
'id' => 'acme',
]);
expect(tenancy()->initialized)->toBeFalse();
pest()->get(route('foo', ['tenant' => 'acme', 'a' => 1, 'b' => 2]));
expect(tenancy()->initialized)->toBeTrue();
expect(tenant('id'))->toBe('acme');
pest()->get(route('baz', ['a' => 1, 'b' => 2]))->assertOk(); // Assert route don't need tenant parameter
});