Please note this package is under development. A working (closed source) multi tenant version is currently in production for laravel 4.1. These packages are an open-source refactoring for version 5 of laravel. If you have questions or wish to participate reach out to me in any way.
This package allows for multi tenancy websites on one installation of Laravel.
The goals for this and its related packages are:
- Unobtrusive multi tenancy for Laravel 5+
- Provide proper insight into tenants and webserver
For more information visit the hyn.me website.
Referring to wikipedia;
Multitenancy refers to a software architecture in which a single instance of a software runs on a server and serves multiple tenants.
In its most abstract sense you can use hyn to manage multiple websites with only one application installation.
- Multiple websites running on one code base.
- Multiple hostnames configured per website.
Each website has its own folder on disk, allowing:
- seperation of routes, templates, translations etc
- custom files (media, themes and packages)
Also each website has its own database, this ensures that in no way one website can access data from another website. The distinction also gives proper division of responsibilities to the system (global) and tenant (local) databases.
For more information visit the hyn.me website.
All packages of hyn (including multi tenancy) require Laravel 5 and up.
Include the dependancy in your composer.json:
composer require hyn-me/multi-tenant
Register the service provider in your config/app.php
within the providers
array:
/*
* HynMe packages
* @info FrameworkServiceProvider will load any available Service Provider from other hyn-me packages
*/
'HynMe\Framework\FrameworkServiceProvider',
Please note this says FrameworkServiceProvider from the Framework package, do not register the MultiTenantServiceProvider!
To support multi tenancy in other (3rd party) packages, replace the class alias for Eloquent under aliases
in your config/app.php
:
'Eloquent' => 'HynMe\Framework\Models\AbstractModel',
This will ensure that all extended classes will by default connect with the tenant database instead of the system database.
If you want to manually connect to the tenant database, set the $connection
property of your class to tenant
.
In your config/database.php
file make sure a database connection is configured with the name hyn
. Also prevent any other connection
listed as tenant
, this package will dynamically create a connection to the tenant database using that config identifier.
The system connection must have the rights to create, alter and delete users and databases and grant rights to others. This behavior is almost identical to a root (admin) user. For security reasons do not configure that user for this connection. You could create such a user manually using:
create database `hyn_multi_tenancy`;
create user `hyn`@'localhost' identified by '<your_strong_random_string>';
grant all privileges on *.* to 'hyn'@'localhost' with grant option;
Using the above snippet you would then add in your config database.php
as hyn
key under connections
:
'hyn' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'hyn_multi_tenancy',
'username' => 'hyn',
'password' => '<your_strong_random_string>',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
In case you're wondering, you can still set the hyn
connection as your default
in the database.php
. In order to be as unobtrusive as possible this is not forced for any hyn package.
As a last step edit your .env
file in the root of your laravel installation and set a default hostname.
HYN_MULTI_TENANCY_HOSTNAME=<hostname>
The entered hostname will be used to fallback if a hostname is hitting on the application that is unknown in the database, thus showing the fallback website. If you don't define this environment variable, a backtrace is generated.
Go into your terminal and run the following artisan command to finish installation of multi tenancy.
php artisan multi-tenant:setup
Please note, if you decide to skip the configuration of the webserver you will have to configure it by yourself. Example files are generated in the storage/webserver
directories.