diff --git a/composer.json b/composer.json index 78357d1c..63196393 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ "require-dev": { "vlucas/phpdotenv": "^3.3", "laravel/framework": "5.8.*", - "orchestra/testbench": "~3.8", + "orchestra/testbench-browser-kit": "~3.8", "league/flysystem-aws-s3-v3": "~1.0", "phpunit/phpcov": "^6.0" }, diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index 09ffeaaf..790e7210 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -7,6 +7,7 @@ use Stancl\Tenancy\Commands\Seed; use Illuminate\Cache\CacheManager; use Stancl\Tenancy\Commands\Install; use Stancl\Tenancy\Commands\Migrate; +use Illuminate\Http\RedirectResponse; use Illuminate\Support\Facades\Route; use Stancl\Tenancy\Commands\Rollback; use Illuminate\Support\ServiceProvider; @@ -47,6 +48,21 @@ class TenancyServiceProvider extends ServiceProvider ]); $this->app->register(TenantRouteServiceProvider::class); + + $this->registerTenantRedirectMacro(); + } + + public function registerTenantRedirectMacro() + { + RedirectResponse::macro('tenant', function (string $domain) { + // replace first occurance of hostname fragment with $domain + $url = $this->getTargetUrl(); + $hostname = \parse_url($url, PHP_URL_HOST); + $position = \strpos($url, $hostname); + $this->setTargetUrl(\substr_replace($url, $domain, $position, \strlen($hostname))); + + return $this; + }); } /** diff --git a/tests/TenantRedirectMacroTest.php b/tests/TenantRedirectMacroTest.php new file mode 100644 index 00000000..de44a650 --- /dev/null +++ b/tests/TenantRedirectMacroTest.php @@ -0,0 +1,23 @@ +name('home'); + + Route::get('/redirect', function () { + return redirect()->route('home')->tenant('abcd'); + }); + + $this->get('/redirect') + ->assertRedirect('http://abcd/foobar'); + } +}