Multi-tenancy is an architecture which allows a single instance of a software application to serve multiple customers. Each client is termed as a tenant. Multi-tenant sites are gaining popularity these days because while building them, maintenance and development costs are shared. Such sites are generally economical. Keeping that in mind, one of our clients wanted us to build a multi-tenant application having multiple databases for each tenant.
vteams engineer used single code base for all tenants in that multi-tenant application using Laravel, but our client had one more requirement i.e. to provide additional functionality to any tenant within the existing modules.
After extensive research, vteams engineer came up with a solution which allowed to extend the functionality of existing modules using extended controllers and routes. This functionality was implemented by following the steps mentioned below:
Create a folder within app folder “app/extended_modules/“. This folder contains all the extended modules with their own controller, models and views. Now, create a route file in this folder “app/extended/modules/routes.php” that will load route files for the extended modules using the following code:
$module = Session::get ('mynamespace');
Now, include route files in the extended modules “app/extended/modules/module1/routes.php” as follows:
Route::group(array('before'=>'api.csrf|site.config' , 'namespace' => 'App\Modules\\'.Session::get('mynamespace').'\Controllers'), function()
This folder also contain extended controllers and views.
Route file is included at the bottom of application’s route (which make routes available at top).
If any request comes with the extended namespace, it will be checked within the extended modules first. If route is found then that functionality will be redeemed from that extended module otherwise the application flow will follow the normal execution.