1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 12:54:05 +00:00

[1.8.0] Tenant redirect (#103)

This commit is contained in:
Samuel Štancl 2019-08-18 12:48:10 +02:00 committed by GitHub
parent 6cee5d36ad
commit b12b9c3a54
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 1 deletions

View file

@ -17,7 +17,7 @@
"require-dev": { "require-dev": {
"vlucas/phpdotenv": "^3.3", "vlucas/phpdotenv": "^3.3",
"laravel/framework": "5.8.*", "laravel/framework": "5.8.*",
"orchestra/testbench": "~3.8", "orchestra/testbench-browser-kit": "~3.8",
"league/flysystem-aws-s3-v3": "~1.0", "league/flysystem-aws-s3-v3": "~1.0",
"phpunit/phpcov": "^6.0" "phpunit/phpcov": "^6.0"
}, },

View file

@ -7,6 +7,7 @@ use Stancl\Tenancy\Commands\Seed;
use Illuminate\Cache\CacheManager; use Illuminate\Cache\CacheManager;
use Stancl\Tenancy\Commands\Install; use Stancl\Tenancy\Commands\Install;
use Stancl\Tenancy\Commands\Migrate; use Stancl\Tenancy\Commands\Migrate;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Commands\Rollback; use Stancl\Tenancy\Commands\Rollback;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
@ -47,6 +48,21 @@ class TenancyServiceProvider extends ServiceProvider
]); ]);
$this->app->register(TenantRouteServiceProvider::class); $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;
});
} }
/** /**

View file

@ -0,0 +1,23 @@
<?php
namespace Stancl\Tenancy\Tests;
use Route;
class TenantRedirectMacroTest extends TestCase
{
/** @test */
public function tenant_redirect_macro_replaces_only_the_hostname()
{
Route::get('/foobar', function () {
return 'Foo';
})->name('home');
Route::get('/redirect', function () {
return redirect()->route('home')->tenant('abcd');
});
$this->get('/redirect')
->assertRedirect('http://abcd/foobar');
}
}