When you develop a multi-tenant application, it's easy for the code of your central application to get entangled with the tenant code. Or you start splitting everything into Central and Tenant folders.
Models |-> Central |-> Tenant
Enter fullscreen mode
Exit fullscreen mode
but there is a better way: modules.
In this post, we will talk about how to do that.
The stack I'm using:
Laravel 13
stancl/tenancy v4
Pest 4
How a module is usually structured
First, we need to decide on a structure. Most developers structure their modules like this:
Modules/Billing/ ├── src/ │ ├── Domain/ │ ├── Application/ │ └── Http/ ├── config/ ├── database/ ├── routes/ └── tests/
Enter fullscreen mode
Exit fullscreen mode
I decided to structure it differently, using the structure you are already used to working with: the default Laravel structure.
modules/ └── Tenancy/ ├── app/ │ ├── Http/ │ │ └── Controllers/ │ │ └── Controller.php │ ├── Models/ │ │ ├── SomeModel.php │ │ └── AnotherModel.php │ └── Providers/ │ ├──…