-
-
Notifications
You must be signed in to change notification settings - Fork 159
[QUESTION] about managing tenants via "landlord" admin panel #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
See it: Use the grouped routes for your landlord domain. |
@masterix21
and I also moved "NeedsTenant" middleware to a separate 'tenant' group. |
Closing this as this is not an issue about the internals of the package. Feel free to keep the conversation going. |
@fermevc see here: #59 (comment) |
@masterix21 class IsLandlord
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($request->getHost() == config('app.domain')) {
// if Host is 'laravel.test' set DB connection to 'landlord'
config(['database.default' => 'landlord']);
return $next($request);
} else {
return $next($request);
}
}
} Whit above in place, all tenant Auth routes and Home route are working OK, tenant switching is working OK, I can get to the landlord welcome and login views, but after submitting login form, 'landlord' connection is present throughout all methods until request comes to "attemptLogin()". protected function attemptLogin(Request $request)
{
dd(config('database.default')); // this still returns 'landlord'
return $this->guard()->attempt(
$this->credentials($request),
$request->filled('remember')
);
} If I comment "dd", I get SQL error because in SQL connector there's no "database" set: |
Sorry, but for me isn’t an excellent way to use a middleware. It’s better to create a database switch task. |
@fermevc I got this working by extending the default NeedsTenant and EnsureValidTenantSession. There probably is a better solution for this. But for now this works. First I used your middleware class LandlordAsFallback
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, \Closure $next)
{
if ($request->getHost() === config('app.domain')) {
// if Host is 'laravel.test' set DB connection to 'landlord'
config(['database.default' => 'landlord']);
return $next($request);
} else {
return $next($request);
}
}
} Creating the LandlordAsFallback middleware is not enough because you will need to make sure that the entire tenant behavior is skipped whenever the landlord domain is requested. That way you can build your own 'application' next to that of the tenant. This probably has some pitfalls I am yet to discover. Extend the NeedsTentant middleware class NeedsTenant extends \Spatie\Multitenancy\Http\Middleware\NeedsTenant
{
public function handle($request, Closure $next)
{
// Skip this middleware whenever the landlord is requested.
if (config('database.default') === 'landlord') {
return $next($request);
}
return parent::handle($request, $next);
}
} Extend the EnsureValidTenantSession class EnsureValidTenantSession extends \Spatie\Multitenancy\Http\Middleware\EnsureValidTenantSession
{
public function handle($request, Closure $next)
{
// Skip this middleware whenever the landlord is requested.
if (config('database.default') === 'landlord') {
return $next($request);
}
return parent::handle($request, $next);
}
} Than this should be the order in protected $middlewareGroups = [
'web' => [
...
\Support\Multitenancy\Middleware\LandlordAsFallback::class,
\Support\Multitenancy\Middleware\NeedsTenant::class,
\Support\Multitenancy\Middleware\EnsureValidTenantSession::class,
]
] Hope this helps someone and if you have a better solution please share :) |
@uteq, thanks for your share. |
I have same issue but these instructions didnt help me. Still getting error: "The request expected a current tenant but none was set." |
Hello, I have tenant domain with subdomain.example.com and the landlord admin pages on app.example.com. Using the code above In tenant mode all routes, login, filesystems etc.. works fine.
I am using an extended Startsession with this code:
What I need to do is to have 2 different login for landlord and tenant with different datas and model for eachother. it seems that when I call app.example.com/login laravel use the tenant route. maybe we need to tell laravel to use the landlord routes ? Thank you for help |
@Franco2911, I think you are using the database session driver with a "tenant" database as the default connection in your |
Just use in the AppServiceProvider boot function if (Tenant::current()==null) {
config(['database.default' => 'landlord']);
} |
After jumping through many hoops with many different packages trying to get a multi-tenancy setup that works great for accessing the landlord app on the base domain and tenants on subdomains. Nova is working brilliantly on both after setting this up. To anyone stuck in a loop trying to work this out. |
Hey guys 👋 So I know this is kind of an old thread, with some different sloutions to slightly different problems. I found it when I was searching for a solution for when I have a Model that is used by both Tenants and Landlords, and had similar issues as above. So I just wanted to share my solution for it, which I think was pretty nice. As you know use should use either
Works great with landlord on main domain, subdomain and tenants on subdomain. |
where can i use this trait ? |
In a model |
I've managed to migrate and seed landlord and two test tenants (tenant1.laravel.test, tenant2.laravel.test).
I would like to have a couple of admin pages for "landlord" but I can't figure out how to do that on my own.
These "landlord" pages would mainly be used for tenant management (simple admin panel for tenant CRUD operations).
I think that because of "NeedsTennant" middleware, I can't serve anything on 'laravel.test' domain. When I try to open 'laravel.test', I get:
I'm aware that this is expected behavior and can't even imagine if what I'm trying to do is possible or even if it should be done this way, mainly because I'm still a beginner.
What would be a proper way to implement "landlord" portion of application that would be accessible via 'laravel.test'.
Thanks in advance!
The text was updated successfully, but these errors were encountered: