简体   繁体   中英

Laravel-Hyn Multi-tenancy, tenant connection setup not seems to resolve tenants and sessions

I am developing a multi-tenant application in php using Laravel 5.7.28, Hyn/Multi-tenant 5.3 in a local host environment. The connection for tenancy is enforced using enforce.tenancy middleware as per the old tutorial. The database connections (config/database.php) is as shown below:(omitted unwanted lines)

'connections' => [

    'sqlite' => [
        'driver' => 'sqlite',
        'database' => env('DB_DATABASE', database_path('database.sqlite')),
        'prefix' => '',
        'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
    ],

    'system' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'tenancy'),
        'username' => env('DB_USERNAME', 'xxxx'),
        'password' => env('DB_PASSWORD', 'xxxxx'),
        ......
    ],

'tenant' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', '914fb6c2sx'),
        'username' => env('DB_USERNAME', 'xxxx'),
       'password' => env('DB_PASSWORD', 'xxxxx'),
        ......
    ],

'tenant' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', '7239b4a43a'),
        'username' => env('DB_USERNAME', 'xxxx'),
        'password' => env('DB_PASSWORD', 'xxxx'),
        ......
    ],

I have set-up sessions (config/sessions.php)

/*
|----------------------------------------
| Session Cookie Domain
|----------------------------------------
|
*/
'domain' => env('SESSION_DOMAIN', '.abcd.in'), 

Now, I am able to login exactly using the same domains user table. If I login in one tenant connection, I am able to go to second domain tenant connection for the same role without login and vice-versa ie if I logout from one domain, I am logged out of the second domain. I am also using Spatie/roles-permissions package. I think I am wrong somewhere in either tenant connection set-up and/or session handling. My code is executing well.

My first doubt of the problem is the tenant connection shown above. In all tenants I am using the connection name as 'tenant'. In hyn/multi-tenant, is the 'tenant' connections an array? I mean it searches for right domain database name in the array of tenant connection? This aspect is not clear to me. If I am wrong to name all connections as tenant then I tried to name tenants as 'tenant1', 'tenant2',...(this set-up did not work). How do one sets-up sessions in this hyn/multi-tenancy. I am trying to use redis but I don't know how to set-up redis in localhost.

I use the hyn package for a multi-tenant app myself, you only need to define the tenant DB connection once, leaving db, user, pass blank, as the hyn package will set the correct details at runtime. In your example, you have 2 tenant db's setup with the same name, which isn't going to work very nicely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM