PHPackages                             dlimars/laravel-tenant-subdomain - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. dlimars/laravel-tenant-subdomain

ActiveLibrary

dlimars/laravel-tenant-subdomain
================================

Pacote para facilitar criação de rotas e troca de banco de dados baseado no subdominio da sua aplicação

v1.0.5(7y ago)251.8k8MITPHP

Since Jan 3Pushed 5y ago4 watchersCompare

[ Source](https://github.com/dlimars/laravel-tenant-subdomain)[ Packagist](https://packagist.org/packages/dlimars/laravel-tenant-subdomain)[ RSS](/packages/dlimars-laravel-tenant-subdomain/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (2)Versions (7)Used By (0)

Laravel Tenant Subdomínio
=========================

[](#laravel-tenant-subdomínio)

[![Build Status](https://camo.githubusercontent.com/e32817e79ddca0f16180c016447d0d5ec269ee33c1f922099e5589727336ed6e/68747470733a2f2f7472617669732d63692e6f72672f646c696d6172732f6c61726176656c2d74656e616e742d737562646f6d61696e2e737667)](https://travis-ci.org/dlimars/laravel-tenant-subdomain)[![Latest Stable Version](https://camo.githubusercontent.com/a8e54a9fd27f0907176f834bbe27756dba46cccfb6f4e6ed9206831e044d56d3/68747470733a2f2f706f7365722e707567782e6f72672f646c696d6172732f6c61726176656c2d74656e616e742d737562646f6d61696e2f762f737461626c65)](https://packagist.org/packages/dlimars/laravel-tenant-subdomain)[![Total Downloads](https://camo.githubusercontent.com/2d6d61b1afe995d4ecc8d14a4d42e8d820f9809a894855dd09981564abf27cc1/68747470733a2f2f706f7365722e707567782e6f72672f646c696d6172732f6c61726176656c2d74656e616e742d737562646f6d61696e2f646f776e6c6f616473)](https://packagist.org/packages/dlimars/laravel-tenant-subdomain)[![Latest Unstable Version](https://camo.githubusercontent.com/9abc8032513a778bd2e3dab20e203656351eea8842a4b2cba83c51c3731b04b5/68747470733a2f2f706f7365722e707567782e6f72672f646c696d6172732f6c61726176656c2d74656e616e742d737562646f6d61696e2f762f756e737461626c65)](https://packagist.org/packages/dlimars/laravel-tenant-subdomain)[![License](https://camo.githubusercontent.com/350f3804eb60fbe7271d0f3d17e1c896a7e580b52228564d523f19082df0633d/68747470733a2f2f706f7365722e707567782e6f72672f646c696d6172732f6c61726176656c2d74656e616e742d737562646f6d61696e2f6c6963656e7365)](https://packagist.org/packages/dlimars/laravel-tenant-subdomain)

Este pacote irá auxiliar na organização de clientes em subdomínios usando Laravel.

Instalação
----------

[](#instalação)

Adicione no seu `composer.json`

```
"require": {
    //..
        "dlimars/laravel-tenant-subdomain": "^1.0"
    },
```

ou execute em seu terminal

```
    composer require dlimars/laravel-tenant-subdomain

```

adicione o provider e o facade em `config/app.php`:

```
'providers' => [
    // outros providers
    Dlimars\Tenant\Providers\TenantServiceProvider::class,
],

'aliases' => [
    // outros aliases
    'Tenant' => Dlimars\Tenant\Facades\Tenant::class,
]
```

adicione o middleware em `app/Http/Kernel.php`

```
protected $routeMiddleware = [
    // outros middlewares
    'tenant.database' => \Dlimars\Tenant\Middlewares\TenantDatabase::class
];
```

Após isso, abra seu console e execute: `php artisan vendor:publish`, modifique o arquivo `config/tenant.php` para sua necessidade, abra seu arquivo `.env` e adicione:

```
APP_HOST=domain.com
TENANT_SUBDOMAIN_ARGUMENT=_account_

```

Uso
---

[](#uso)

para gerar rotas de subdominio, utilize da seguinte forma:

```
// Tenant::getFullDomain() retorna algo como '{_account_}.domain.com'

Route::group(['domain' => Tenant::getFullDomain()], function () {
    Route::get('subdomain-teste/{id}', ['as' => 'subdomain-teste', function($subdomain, $id){
        return route('subdomain-teste', ['123']);
    }]);
});
```

para gerar rotas para a aplicação principal (que não seja subdominio), utilize da seguinte forma

```
// Tenant::getDomain() retorna algo como 'domain.com'

Route::group(['domain' => Tenant::getDomain()], function () {
    Route::get('domain-teste/{id}', ['as' => 'domain-teste', function($id){
        return route('domain-teste', ['123']);
    }]);
});

// isso impede que rotas do dominio possam ser acessadas através do subdominio
```

Carregando as configurações de banco de acordo com o subdominio
---------------------------------------------------------------

[](#carregando-as-configurações-de-banco-de-acordo-com-o-subdominio)

os arquivos de configurações de banco serão lidos por padrão, dentro da pasta `config/tenant`, com o exemplo de conteudo:

```
return [
    'driver'    => 'mysql',
    'host'      => 'host',
    'database'  => 'db_subdomain',
    'username'  => 'user_subdomain',
    'password'  => 'user_password',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
];
```

o arquivo é lido e adicionado como conexão padrão `tenant`, isso é feito via Middleware, em todas as rotas que irão utilizar base de dados própria, use o middleware `tenant.database`:

```
Route::group(['domain' => Tenant::getFullDomain(), 'middleware' => ['tenant.database']], function () {
    Route::get('domain-teste/{id}', ['as' => 'domain-teste', function($id){
        return route('domain-teste', ['123']);
    }]);
});
```

Supondo que o usuário acesse `http://beltrano.domain.com`, a configuração a ser carregada deverá estar em `/config/tenants/beltrano.php` (isso é configurável)

### Criar configurações de banco

[](#criar-configurações-de-banco)

Para criar uma nova configuração de banco, use da seguinte forma:

```
$config = [
    'foo' => 'bar'
];

Tenant::makeDatabaseConfigFile('foo', $config);
```

isso irá gerar um arquivo dentro de `config/tenants` com o nome de `foo.php` (ou como/onde for definido na configuração), com o seguinte conteúdo

```
return [
    'foo' => 'bar'
];
```

### Excluir configurações de banco

[](#excluir-configurações-de-banco)

Para excluir um arquivo de configuração, apenas execute da seguinte maneira:

```
Tenant::dropDatabaseConfigFile('foo');
```

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 86.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~173 days

Recently: every ~217 days

Total

6

Last Release

2863d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ed522ebce0f7d79672f00edda86cc75a50059aff782c3380747e08fb8b9620b4?d=identicon)[dlimars](/maintainers/dlimars)

---

Top Contributors

[![dlimars](https://avatars.githubusercontent.com/u/4266198?v=4)](https://github.com/dlimars "dlimars (38 commits)")[![brunopmaia](https://avatars.githubusercontent.com/u/17571341?v=4)](https://github.com/brunopmaia "brunopmaia (4 commits)")[![bernardomacedo](https://avatars.githubusercontent.com/u/5430688?v=4)](https://github.com/bernardomacedo "bernardomacedo (1 commits)")[![vinicius73](https://avatars.githubusercontent.com/u/1561347?v=4)](https://github.com/vinicius73 "vinicius73 (1 commits)")

### Embed Badge

![Health badge](/badges/dlimars-laravel-tenant-subdomain/health.svg)

```
[![Health](https://phpackages.com/badges/dlimars-laravel-tenant-subdomain/health.svg)](https://phpackages.com/packages/dlimars-laravel-tenant-subdomain)
```

###  Alternatives

[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9552.1M82](/packages/roots-acorn)[psalm/plugin-laravel

Psalm plugin for Laravel

3254.8M282](/packages/psalm-plugin-laravel)[omniphx/forrest

A Laravel library for Salesforce

2744.3M8](/packages/omniphx-forrest)[lkaemmerling/laravel-horizon-prometheus-exporter

A small package to gain and export long time information from Laravel &amp; Horizon for Prometheus.

1601.9M](/packages/lkaemmerling-laravel-horizon-prometheus-exporter)[melihovv/laravel-log-viewer

A Laravel log viewer

1231.5k1](/packages/melihovv-laravel-log-viewer)[pingpong/facebook

Facebook OAuth v4 for Laravel Framework

1129.7k](/packages/pingpong-facebook)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
