Package Data | |
---|---|
Maintainer Username: | gpopoteur |
Maintainer Contact: | mail@gpopoteur.com (German Popoter) |
Package Create Date: | 2015-04-08 |
Package Last Update: | 2017-03-30 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-14 15:12:57 |
Package Statistics | |
---|---|
Total Downloads: | 18 |
Monthly Downloads: | 1 |
Daily Downloads: | 0 |
Total Stars: | 16 |
Total Watchers: | 5 |
Total Forks: | 2 |
Total Open Issues: | 0 |
WORK IN PROGRESS!!
Laravel 5 package for creating multi-tenant apps. As of right now only Postgresql Schemas and SQLite Databases are supported.
Install via composer with the command:
composer require gpopoteur/flat
Then register the provider in the config/app.php
file:
'GPopoteur\Flat\FlatServiceProvider',
After that you can start using the Flat
API! :)
The Database driver used by flat
will be the same one specified in config/database.php
, meaning the same that you are using for your application.
You can inject the dependency with Laravel IoC container:
public function __contruct(Flat $flat){
// business logic
}
Or just let the App::make()
resolve the class.
$flat = App::make('GPopoteur\Flat\Flat');
To create a new Flat (Tenant), just call the build($name)
method of the Flat
class passing the new Tenant name.
$flat->build('new-tenant');
After you create a new tenant, the new Schema is not automatically migrated, to run the migrations just run:
$flat->migrate('new-tenant');
or, if migrating several tenants at once, pass an array with the tenants names:
$flat->migrate(['new-tenant', 'other-tenant', 'and-another']);
To change the Tenant programatically you can call the moveIn($name)
method of the Flat
API.
$flat->moveIn('new-tenant');
There is a middleware implemented called FlatCheckInMiddleware
, basically what it does is to take the name of the variable flatName
and change the user to that Schema.
To register this middleware you need to add the following line to the $routeMiddleware
variable in the app/Http/Kernel.php
file:
'flatCheckIn' => 'GPopoteur\Flat\Middleware\FlatCheckInMiddleware'
and you should be ready to go.
Example:
To be able to do a subdomain tenant, just add a flatName
variable and the flatCheckIn
middleware to your domain route group.
Route::group(['domain' => '{flatName}.domain.com', 'middleware' => 'flatCheckIn'], function(){
// everything inside this closure will be done in the `flatName` schema.
});
Because the flatName
variable is assigned in the route, you can add that variable to any route group that fits your need, example:
Route::group(['prefix' => 'account/{flatName}', 'middleware' => 'flatCheckIn'], function(){
// everything inside this closure will be done in the `flatName` schema.
});
When using the provided Middlewares, if a tenant doesn't exists the middleware will throw a new FlatDoesntExistsException
that you can catch globally in the app and then redirect the user somewhere and show then a nice error message.
Contributions to the library are more than welcome. To contribute code to this repo, follow this simple steps:
feature/this-thing
develop
branch of this repo.Thanks :)
Because this package works with the database, and the data is the more critical part of a production app I make the following statement:
I am not responsible in any way for any harm that this library does to your data. This package comes as-is. Use at your own risk.
MIT