diff --git a/assets/TenancyServiceProvider.stub.php b/assets/TenancyServiceProvider.stub.php index 49ed0d39..87198a06 100644 --- a/assets/TenancyServiceProvider.stub.php +++ b/assets/TenancyServiceProvider.stub.php @@ -137,6 +137,12 @@ class TenancyServiceProvider extends ServiceProvider ]; } + /** + * Set \Stancl\Tenancy\Bootstrappers\RootUrlBootstrapper::$rootUrlOverride here + * to override the root URL used in CLI while in tenant context. + * + * @see \Stancl\Tenancy\Bootstrappers\RootUrlBootstrapper + */ protected function overrideUrlInTenantContext(): void { /** @@ -149,12 +155,12 @@ class TenancyServiceProvider extends ServiceProvider * * $scheme = str($originalRootUrl)->before('://'); * - * // If you're using subdomain identification: - * // $originalDomain = str($originalRootUrl)->after($scheme . '://'); - * // return $scheme . '://' . $tenantDomain . '.' . $originalDomain . '/'; - * * // If you're using domain identification: * return $scheme . '://' . $tenantDomain . '/'; + * + * // If you're using subdomain identification: + * $originalDomain = str($originalRootUrl)->after($scheme . '://'); + * return $scheme . '://' . $tenantDomain . '.' . $originalDomain . '/'; * }; */ } diff --git a/src/Bootstrappers/RootUrlBootstrapper.php b/src/Bootstrappers/RootUrlBootstrapper.php index 08142e0f..6a523673 100644 --- a/src/Bootstrappers/RootUrlBootstrapper.php +++ b/src/Bootstrappers/RootUrlBootstrapper.php @@ -6,25 +6,47 @@ namespace Stancl\Tenancy\Bootstrappers; use Closure; use Illuminate\Config\Repository; +use Illuminate\Contracts\Foundation\Application; use Illuminate\Routing\UrlGenerator; use Stancl\Tenancy\Contracts\TenancyBootstrapper; use Stancl\Tenancy\Contracts\Tenant; +/** + * While using CLI, automatically alter the root URL used by the URL generator (affects calls like url('/') and route('foo')). + * + * Example: + * Your app's URL (env('APP_URL') / config('app.url') -- the root URL) is http://localhost, + * you have a tenant with a single subdomain ('acme'), + * and you want to use that domain as the tenant's 'primary' domain. + * + * Using a closure like the one provided in the overrideUrlInTenantContext() method example in TenancyServiceProvider + * as the $rootUrlOverride property of this class, you can make the URL generator use + * http://acme.localhost instead of http://localhost as the root URL during the URL generation while in the tenant's context. + * Meaning, `url('/foo')` (or `URL::to('/foo')`) will return http://acme.localhost/foo. + */ class RootUrlBootstrapper implements TenancyBootstrapper { + /** + * A closure that accepts the tenant and the original root URL and returns the new root URL. + * When null, the root URL is not altered in any way. + * + * We recommend setting this property in the TenancyServiceProvider's overrideUrlInTenantContext() method. + */ public static Closure|null $rootUrlOverride = null; + protected string|null $originalRootUrl = null; public function __construct( protected UrlGenerator $urlGenerator, protected Repository $config, + protected Application $app, ) {} public function bootstrap(Tenant $tenant): void { - $this->originalRootUrl = $this->urlGenerator->to('/'); + if ($this->app->runningInConsole() && static::$rootUrlOverride) { + $this->originalRootUrl = $this->urlGenerator->to('/'); - if (static::$rootUrlOverride) { $newRootUrl = (static::$rootUrlOverride)($tenant, $this->originalRootUrl); $this->urlGenerator->forceRootUrl($newRootUrl); @@ -34,7 +56,9 @@ class RootUrlBootstrapper implements TenancyBootstrapper public function revert(): void { - $this->urlGenerator->forceRootUrl($this->originalRootUrl); - $this->config->set('app.url', $this->originalRootUrl); + if ($this->originalRootUrl) { + $this->urlGenerator->forceRootUrl($this->originalRootUrl); + $this->config->set('app.url', $this->originalRootUrl); + } } }