These docs are for v2.0. Click to read the latest docs for v5.

Tenant identification

Explanation of how the tenancy package identifies the tenant.

1. Service provider registration

Hyn multi tenancy hooks itself into the Laravel ecosystem using the hyn/framework package. This package has a FrameworkServiceProvider that automatically loads all required service providers, even those of possible add ons.

At this point only two service providers are used; Tenancy and Webserver. These service providers will be automatically registered and booted when needed.

2. Redirect middleware (optional)

The hostname object allows for several configurations to be set. Amongst others that redirection to another hostname occurs. In order for this redirection to work the middleware HostnameMiddleware of the multi-tenant package is added to all routes on runtime.

📘

Configurable

You can disable this middleware from the multi-tenant configuration file.

3. Service providers booting

Most of the magic happens when the service providers boot, especially the TenancyServiceProvider handles the majority of the runtime configuration for tenancy:

  • Identify what the hostname is of the request (ignores artisan for instance).
  • Query the system database for any matching hostnames.
  • Load necessary related information of this hostname, including the website details.
  • If for this website a storage folder exists, sets up each additional piece of code, like routes etc.
  • Also set up the database connection "tenant" to allow connecting to the tenant database.

3.A Tenant identification

The identification of the actual Tenant is quite straight forward. There are two use cases, web and non-web.

In Web transactions (browser interactions that use an URL), the requested domain is identified using Laravel's Request::getHost() and doing a lookup in the hostnames table using the Eloquent model Hostname.

In non-Web transactions (terminal/ssh) we are unable to receive hostnames, in these circumstances the applicable commands need a --tenant flag. So far some of the migrations commands have been given this flag. The --tenant flag always allows for one integer value, relating to a website Id or it can hold all for every tenant in the system database.