Laravel swoole caching config for database (multitenancy)

I have a simple multitenancy logic in Laravel, it works like this:

  • Every client has a different subdomain attach to it
  • I have a connection called “tenant” configured on my databases config
  • With every request I see what is the subdomain, and fill the database of the “tenant” connection

To achieve this, I have overriden the DatabaseManager and DatabaseServiceProvider, and changed the “connection” method to check the subdomain, like the code below.

Basically it works perfectly with nginx, and my project is running for years without problems.

The issue is: I’m developing a websocket on this project, and for that I’m using Laravel Swoole. I configured my nginx to be only the proxy for the project, and the websocket is working, but when I tested the multitenancy it doesn’t work.

Laravel Swoole seems to read the first configuration and then it doesn’t change anymore.

Explaining my approach to multitenancy:
With older versions of Laravel I had a simpler approach that was only a middleware that did the same thing, on newer ones (I’m using Laravel 8 on this project) I had to do this, because I couldn’t get the Middleware approach was to work, it seemed to open connection to the database before reaching the middleware, so I configured on the “root”. I’m explaining this because I’m not sure if this is the problem, and if there is a more correct approach for doing this. I tried to read some of the open source solutions, but it was too complicated for my needs.

This is basically the code:


class CustomDatabaseManager 
{
    private function getSubdomain()
    {
        return explode('.', parse_url(request()->url(), PHP_URL_HOST))[0];
    }

    public function connection($name = null)
    {
        [$database, $type] = $this->parseConnectionName($name);

        $name = $name ?: $database;

        if ($name == 'tenant')
        {

            $subdomain = $this->getSubdomain();
            
            $tenant = Tenant::where('referencia', $subdomain)->first();

            if ($tenant != null)
            {
                $this->app['config']["database.connections.tenant.database"] = $tenant->pgsql_bd;
            }
        }

        // If we haven't created this connection, we'll create it based on the config
        // provided in the application. Once we've created the connections we will
        // set the "fetch mode" for PDO which determines the query return types.
        if (! isset($this->connections[$name])) {
            $this->connections[$name] = $this->configure(
                $this->makeConnection($database), $type
            );
        }

        return $this->connections[$name];
    }
}

I’ve tried:

  • First I created a function that changes the database in an isolated manner, so I can control when it triggers.
  • I’ve created a route that connects to the first tenant, echoes first record of table “config” then tries to change the database connection to a second tenant and echoes the config again, in both echoes it shows the first tenant data.

I’ve also tried:

  • Changing the config in other ways
  • Changing the cache in many ways possible
  • Deleting/Purging connections before and after setting it
  • Reseting the repository with swoole
  • Changing many configurations in swoole by trial and error

I just want to change the connection and read the second database.

New contributor

Victor Hugo V. da Cunha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật