3 Comments
User's avatar
Prosper Sosu's avatar

I love the simplicity of your articles. I’m particularly interested in implementing multi tenants in some of my projects (especially laravel) by doing it myself instead of using packages.

If can dive a bit further into implementing multi tenant using custom codes it will be a great read. Focusing on the main pillars!

Gracias!

Expand full comment
Michał Kurzeja's avatar

Thanks! Appreciate the feedback.

I think doing it by yourself will be beneficial in many areas. Especially, you can learn how such an application works.

Not sure how it will be done in Laravel, but in Symfony the easiest flow is to:

1. Leverage request events to process and detect the tenant, set specific request data to make it available for further processing.

2. Use doctrine filters to spot entities that are tenant aware, and set specific filters.

3. Or as an alternative, switch Doctrine DB connection based on the tenant, depending on the data isolation pattern.

Those are the basics. I know Laravel has similar possibilities. Several people shared their approaches in a Reddit topic: https://www.reddit.com/r/PHP/comments/1lkttfj/saas_with_php_libraries_or_roll_your_own/

Expand full comment
Prosper Sosu's avatar

Very much appreciated. Over the past days I have been able to implement one with separate pgSQL/ Schema based in laravel.

In laravel one can leverage AppServiceProvider to early detect the tenant (either by domain, subdomain or some request pattern). Once the tenant is detected, switch database/switch tenant etc. a middleware could also be used in other places.

But of course there other things to consider and work on. But understand how it works, for me is important. It makes it easier to even use other packages.

Danke!

Expand full comment