1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 16:34:04 +00:00
This commit is contained in:
Abrar Ahmad 2022-08-09 10:18:07 +05:00
parent db4a795c3e
commit 25f19c2c39
2 changed files with 26 additions and 2 deletions

View file

@ -3,6 +3,7 @@
declare(strict_types=1);
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Resolvers\PathTenantResolver;
use Stancl\Tenancy\Tenancy;
if (! function_exists('tenancy')) {
@ -59,7 +60,7 @@ if (! function_exists('global_cache')) {
if (! function_exists('tenant_route')) {
function tenant_route(string $domain, $route, $parameters = [], $absolute = true)
{
// replace first occurance of hostname fragment with $domain
// replace first occurrence of hostname fragment with $domain
$url = route($route, $parameters, $absolute);
$hostname = parse_url($url, PHP_URL_HOST);
$position = strpos($url, $hostname);
@ -67,3 +68,15 @@ if (! function_exists('tenant_route')) {
return substr_replace($url, $domain, $position, strlen($hostname));
}
}
if (! function_exists('tenant_path_route')) {
function tenant_path_route($route, $parameters = [])
{
if (! array_key_exists(PathTenantResolver::$tenantParameterName, $parameters)) {
$parameters[PathTenantResolver::$tenantParameterName] = optional(tenant())->getTenantKey();
}
dd($parameters);
return route($route, $parameters);
}
}

View file

@ -18,7 +18,7 @@ beforeEach(function () {
], function () {
Route::get('/foo/{a}/{b}', function ($a, $b) {
return "$a + $b";
});
})->name('foo');
});
});
@ -123,3 +123,14 @@ test('tenant parameter name can be customized', function () {
->withoutExceptionHandling()
->get('/acme/foo/abc/xyz');
});
test('tenant path route helper function test', function () {
Tenant::create([
'id' => 'acme',
]);
pest()->get( tenant_path_route('foo', ['a' => 'a', 'b' => 'b']));
expect(tenancy()->initialized)->toBeTrue();
expect(tenant('id'))->toBe('acme');
});