tenancy icon indicating copy to clipboard operation
tenancy copied to clipboard

Cross-domain redirect not working before initialize tenancy

Open sunxyw opened this issue 2 years ago • 1 comments

Bug description

I am trying to redirect the user to the tenant subdomain from the central domain.

I wrote return redirect()->route('tenant::home')->domain('sub1.dev.local'); in my central controller, but this will raise an exception Call to undefined method Illuminate\Http\RedirectResponse::domain().

After researching, I found that the relevant feature is registered after Tenancy is called, so if Tenancy is never called, the domain method will not be added. https://github.com/archtechx/tenancy/blob/1ea3cefa1d1499211e8659a0b2e050b05ff0c4fe/src/TenancyServiceProvider.php#L31-L37

Steps to reproduce

// routes/web.php
Route::get('/', function () {
//    tenancy();
    return redirect()->route('tenant::home')->domain('sub1.dev.local');
})->name('home');

It should raise a BadMethodCallException when you access it.

After uncomment the // tenancy(), it should works.

Expected behavior

Optional features should be bootstrapped no matter the state of Tenancy initialization.

Or we should provide options for choosing which features to initialize ahead of time.

Laravel version

9.30.1

stancl/tenancy version

3.5.9

sunxyw avatar Sep 16 '22 09:09 sunxyw

Thanks for reporting this. We should move this to a different point in the lifecycle, though I'm not sure what point exactly. If we moved this to the TenancyServiceProvider's boot() method, then I think it wouldn't respect changes made to config() e.g. in tests. So it should be a bit later after the app is initialized, but without requiring tenancy() to be used.

stancl avatar Sep 19 '22 03:09 stancl

Closing this as we have an open PR with a fix

stancl avatar Feb 20 '23 23:02 stancl