From 4df358fe3a922ee5d087c00ea06a26a1aafdecec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 20 Nov 2019 08:15:28 +0100 Subject: [PATCH] Add tenant_route helper --- src/helpers.php | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/helpers.php b/src/helpers.php index aacf5fbe..101d43df 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -5,7 +5,7 @@ declare(strict_types=1); use Stancl\Tenancy\Tenant; use Stancl\Tenancy\TenantManager; -if (! \function_exists('tenancy')) { +if (! function_exists('tenancy')) { /** @return TenantManager|mixed */ function tenancy($key = null) { @@ -17,7 +17,7 @@ if (! \function_exists('tenancy')) { } } -if (! \function_exists('tenant')) { +if (! function_exists('tenant')) { /** @return Tenant|mixed */ function tenant($key = null) { @@ -29,7 +29,7 @@ if (! \function_exists('tenant')) { } } -if (! \function_exists('tenant_asset')) { +if (! function_exists('tenant_asset')) { /** @return string */ function tenant_asset($asset) { @@ -37,16 +37,30 @@ if (! \function_exists('tenant_asset')) { } } -if (! \function_exists('global_asset')) { +if (! function_exists('global_asset')) { function global_asset($asset) { return app('globalUrl')->asset($asset); } } -if (! \function_exists('global_cache')) { +if (! function_exists('global_cache')) { function global_cache() { return app('globalCache'); } } + +if (! function_exists('tenant_route')) { + function tenant_route(string $route, array $parameters = [], string $domain = null): string + { + $domain = $domain ?? request()->getHost(); + + // replace first occurance of hostname fragment with $domain + $url = route($route, $parameters); + $hostname = parse_url($url, PHP_URL_HOST); + $position = strpos($url, $hostname); + + return substr_replace($url, $domain, $position, strlen($hostname)); + } +}