You are currently viewing Extend Multi-Tenant Site Functionality using Routes in Laravel
vteams- blog

Extend Multi-Tenant Site Functionality using Routes in Laravel

  • Post published:January 5, 2016

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:

Step 1

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:


Route::group([],function() {
     $module = Session::get ('mynamespace');

        if(file_exists(__DIR__.'/'.$module.'/routes.php')) {
            include __DIR__.'/'.$module.'/routes.php';


Step 2

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.

Step 3

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.