multitenancy-utils-laravel maintained by italiavainrete
MultiTenancy Utils for Laravel
laravel/multitenancy-utils-laravel is a package designed to simplify multi-tenancy management in Laravel applications. It provides tools to handle tenant-specific configurations, assets, branding, and more.
Installation
To install the package, use composer:
composer require laravel/multitenancy-utils-laravel
Configuration
Most of the options can be customized using .env variabiles, however you can publish the config file with:
php artisan vendor:publish --tag="multitenancy-utils-laravel-config"
This is the contents of the published config file config/multitenancy-utils-laravel.php, modify it to suit your needs:
return [
'tenant_key' => env('TENANT_KEY','italia-va-in-rete'),
'crm-api' => [
'base_url' => env('CRM_API_BASE_URL', 'https://api.crm.italiavainrete.it')
],
'cache' => [
'ttl' => env('CACHE_TTL', 600),
],
'colors' => [
'lighten_percentage' => env('COLORS_LIGHTEN_PERCENTAGE', 40),
'darken_percentage' => env('COLORS_DARKENEN_PERCENTAGE', 15),
],
'cdn' => env('CDN_BASE_URL', 'https://d3vk0yr71svhiq.cloudfront.net/ivr'),
];
Usage
Controllers
Two routes are provided for serving tenant-specific web manifest and browser config XML:
Route::get('/site.webmanifest', WebManifestController::class)->name('web-manifest');
Route::get('/browserconfig.xml', BrowserConfigXmlController::class)->name('browser-config-xml');
Getting Started
The package aims to simplify rendering of a series of common tags to define the style of a page using data from the tenant's brand
View Components
You can use the following view components provided by the package:
x-multi-tenancy::brand-favicons- Renders tenant favicon meta tags.x-multi-tenancy::brand-style- Renders styles for tenant.x-multi-tenancy::brand-logo- Displays tenant logo.
Full Usage Example
Here's a full example of a Blade template using the components and shared brand data:
<!DOCTYPE html>
<html>
<head>
<title>{{ $brand->name }} Marketplace</title>
<!-- Renders Tenant Favicon meta tags -->
<x-multi-tenancy::brand-favicons />
<!-- Render Styles for Tenant -->
<x-multi-tenancy::brand-style />
</head>
<body class="themed-font antialiased">
<div class="min-h-screen bg-gray-100 dark:bg-gray-900">
<!-- Background Tenant colors -->
<header class="bg-primary">
<!-- Tenant Logo -->
<x-multi-tenancy::brand-logo />
</header>
<main>
<!-- Text, Border and Background tenant primary/secondary and semantic colors including light/dark variations -->
<h1 class="text-primary border-primary bg-primary-light">Hello World</h1>
<p class="text-success border-success bg-success-light">Success!</p>
<p class="text-danger border-danger bg-danger-light">Danger!</p>
<p class="text-warning border-warning bg-warning-light">Warning!</p>
<p class="text-info border-info bg-info-light">Info!</p>
</main>
</div>
</body>
</html>
Testing
composer test
License
The MIT License (MIT). Please see License File for more information.
Invalid OAuth tokens
UserApiService::getUserData() throws IVR\MultiTenancyUtils\Exceptions\InvalidUserTokenException
when the user area rejects the token (401/403, empty or malformed profile, missing token).
If the user area is down (5xx, 429, timeout) it returns a placeholder user and keeps the session.
Lookups are memoized for the current request.
Append the middleware to the web group so a user with a dead token is logged out
(oauth_token and remember_token cleared, session invalidated) instead of getting a 500:
// bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
$middleware->web(append: \IVR\MultiTenancyUtils\Http\Middleware\EnsureValidUserToken::class);
})
The middleware is also aliased as ivr.user-token. By default the page is then served as a guest;
set multitenancy-utils-laravel.user_token.on_invalid to redirect to send the user to
user_token.redirect_route (default login).
On routes protected by auth the middleware throws AuthenticationException
after the logout, so the user gets the usual redirect to login (401 for JSON).
user_token.validation_ttl (seconds, default 0) caches successful validations
to avoid calling the user area on every request.