A Laravel package that allows you to use multiple ".env" files in a precedent manner. Use ".env" files per domain (multi-tentant)!

Overview

Laravel Multi ENVs

Social Card of Laravel Multi ENVs

PHP Version Laravel Version CI Status PHPCS - GitHub Workflow Status PHPMD - GitHub Workflow Status PHPStan - GitHub Workflow Status Coverage Status Latest Version MIT Licensed

Use multiple .envs files and have a chain of precedence for the environment variables in these different .envs files. Use the .env file in a custom way to manipulate environment variables by domain (multi-tenant).

๐Ÿš€ Installation

Requirements

The package has been developed and tested to work with the following minimum requirements:

  • PHP 8.0
  • Laravel 9.0

Laravel version Compatibility

Laravel PHP Package
9.x 8.0 ^2.0
8.x 7.4 ^1.0

Install the Package

You can install the package via Composer:

composer require allysonsilva/laravel-multienv

Publish the Config

You can then publish the package's config file by using the following command:

php artisan vendor:publish --tag="envs-config"

๐Ÿ”ง Configuration

  1. Add trait to kernel console app/Console/Kernel.php:

    <?php
    
    namespace App\Console;
    
    use Illuminate\Console\Scheduling\Schedule;
    +use Allyson\MultiEnv\Concerns\ConsoleCallTrait;
    +use Allyson\MultiEnv\Concerns\BootstrappersTrait;
    use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
    
    class Kernel extends ConsoleKernel
    {
    +    use BootstrappersTrait, ConsoleCallTrait;
  2. Add trait to kernel http app/Http/Kernel.php:

    <?php
    
    namespace App\Http;
    
    +use Allyson\MultiEnv\Concerns\BootstrappersTrait;
    use Illuminate\Foundation\Http\Kernel as HttpKernel;
    
    class Kernel extends HttpKernel
    {
    +    use BootstrappersTrait;

๐Ÿ“– Usage

The use of this package happens by manipulating the .envs files in the project.

Using multiple .env files in the project root

  • You can use as many .env files as you like.

  • By default, when listing .env files to give priority to the last ones, the "natural order" algorithm is used, more specifically PHP's strnatcmp function, thus the environment variables of the last .envs will have priority / precedence over all others.

    • You can use the config('envs.sorted') to custom sort the .envs files. The last items in the array will have priority over the others.
  • Use the config('envs.ignored') regex to ignore .env files that should not be processed/handled.

See it in action

Assuming we have 3 .env files in the root of the application with their environment variables as follows:

.
โ”œโ”€โ”€ app
โ”œโ”€โ”€ bootstrap
โ”œโ”€โ”€ config
โ”œโ”€โ”€ database
โ”œโ”€โ”€ envs
โ”‚   โ”œโ”€โ”€ .env.site1.test
โ”‚   โ””โ”€โ”€ .env.site2.test
โ”œโ”€โ”€ lang
โ”œโ”€โ”€ public
โ”œโ”€โ”€ resources
โ”œโ”€โ”€ routes
โ”œโ”€โ”€ storage
โ”œโ”€โ”€ tests
โ”œโ”€โ”€ .env
โ”œโ”€โ”€ .env.example
+โ”œโ”€โ”€ .envA
+โ”œโ”€โ”€ .envB
+โ”œโ”€โ”€ .envC
โ”œโ”€โ”€ .gitattributes
โ”œโ”€โ”€ .gitignore
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ artisan
โ”œโ”€โ”€ composer.json
โ””โ”€โ”€ phpunit.xml

Obs: The envs folder in the application structure below will be explained later.

Each .env file has its environment variables set as follows:

.envA:

ENV_NAME="NAME Env A"
APP_URL=http://env-a.test

ENV_FILE_A=.envA

.envB:

ENV_NAME="NAME Env B"
APP_URL=http://env-b.test

ENV_FILE_B=.envB

.envC:

ENV_NAME="NAME Env C"
APP_URL=http://env-c.test

ENV_FILE_C=.envC

As it is, the .envC file is the last one listed in the structure above, so it will override any environment variables defined in the preceding .env files, and the environment variables that exist in the other files. more is not in the last (priority), it will continue to be used normally, the result / consolidated of the environment variables of the three files are:

ENV_NAME="NAME Env C"
APP_URL=http://env-c.test

ENV_FILE_A=.envA
ENV_FILE_B=.envB
ENV_FILE_C=.envC

Using the configuration of config('envs.sorted'), you can customize the default order of file priorities:

'sorted' => [
    '.envA',
    '.envC',
    '.envB',
],

As above, the result of the environment variables of the 3 files would be:

ENV_NAME="NAME Env B"
APP_URL=http://env-b.test

ENV_FILE_A=.envA
ENV_FILE_C=.envC
ENV_FILE_B=.envB

Using multiple .env files per domain

In the same way that you use multiple envs files in the root of the application, it is possible to use .env files per domain:

  • These files are located in the configuration folder of config('envs.folder'), which by default the folder name is envs, as seen in the project listing above.

  • In order for the .env file to match the domain / subdomain, it must be created as follows: .env.<domain>.

  • To configure a different .env filename than the default, add the domain to config('envs.domains'), having the env key of the domain set to your preference.

See it in action

Using the same structure as the previous example, if the request / domain is site1.test, and there is a file .env.site1.test inside the envs folder, then the environment variables in that file will override all the other environment variables.

Assuming that the .env.site1.test file has the following variables:

ENV_NAME="NAME Env SITE 1"
APP_URL=http://site1.test

When environment variables are used in the project, they will have the following results:

ENV_NAME="NAME Env SITE 1"
APP_URL=http://site1.test

ENV_FILE_A=.envA
ENV_FILE_C=.envC
ENV_FILE_B=.envB

To see the examples in action, let's use this laravel application.

Domain custom .env file name

When the domain's .env filename is different from the default which is: .env.<domain>, then set the env key in the domain configs in config('envs.domains') , as follows:

'domains' => [
    'your-domain.tld' => [
        'env' => '.env.custom-name',
    ],
],

Using config:cache and route:cache per domain

You can cache configs and routes by domain.

Caching configs by domain - config:cache

A new --domain option is available in the command. Using this option, the environment variables from the domain's .env file in the envs folder will override and take precedence over all others.

Use the config('envs.domains') configuration to customize the .php file that will be saved and used as a cache of configs. This ensures that multiple configuration files per domain can exist and be used in the same project.

To generate and use the .php file with a custom name of the domain cache settings, use the following code as an example:

'domains' => [
    'site2.test' => [
        'APP_CONFIG_CACHE' => 'config-site2-test.php',
    ],
],

Caching routes by domain - route:cache

As in the section above, the command to create the route cache file has a new --domain option, which will be used to filter only the routes that have the domain according to the option value.

It is also possible to have a custom name for the routes cache file, such as the configuration cache, is through the APP_ROUTES_CACHE key, as in the example below:

'domains' => [
    'site2.test' => [
        'APP_ROUTES_CACHE' => 'routes-v7-site2-test.php',
    ],
],

๐Ÿงช Testing

composer test:unit

๐Ÿ“ Changelog

Please see CHANGELOG for more information about the changes on this package.

๐Ÿค Contributing

Please see CONTRIBUTING for details.

๐Ÿ”’ Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

๐Ÿ† Credits

License

The MIT License (MIT). Please see License File for more information.

You might also like...
Per-user settings repository system for Laravel
Per-user settings repository system for Laravel

Laraconfig Per-user settings repository system for Laravel. This package allows users to have settings that can be queried, changed and even updated,

This package provides a Logs page that allows you to view your Laravel log files in a simple UI
This package provides a Logs page that allows you to view your Laravel log files in a simple UI

A simplistics log viewer for your Filament apps. This package provides a Logs page that allows you to view your Laravel log files in a simple UI. Inst

.env vars check for Spatie's Laravel Health

Custom check for Spatie's Laravel Health - Ensure every .env variable you need has a value

This Laravel Nova settings tool based on env, using nativ nova fields and resources
This Laravel Nova settings tool based on env, using nativ nova fields and resources

Nova Settings Description This Laravel Nova settings tool based on env, using nativ nova fields and resources Features Using native Nova resources Ful

Source Code for 'Domain-Driven Laravel' by Jesse Griffin

Apress Source Code This repository accompanies Domain-Driven Laravel by Jesse Griffin (Apress, 2020). Download the files as a zip using the green butt

Create and manage A Domain Driven Design (DDD) in your Laravel app, simply and efficiently.

Create and manage A Domain Driven Design (DDD) in your Laravel app, simply and efficiently.

If you are beginner in WordPress plugin development or if you want to develop your own store product plugin you use this plugin
If you are beginner in WordPress plugin development or if you want to develop your own store product plugin you use this plugin

hirwa-products-plugin If you are beginner in WordPress plugin development or if you want to develop your own store product plugin you use this plugin

A lightweight domain event pattern implementation for Doctrine2.
A lightweight domain event pattern implementation for Doctrine2.

Knp Rad Domain Event A lightweight domain event pattern implementation for Doctrine2. Official maintainers: @Einenlum Installation With composer : $ c

Allows you to use Twig seamlessly in Laravel

Allows you to use Twig seamlessly in Laravel. Requirements TwigBridge = 0.13 supports Twig 3. If you need Twig 1/2 support, use the 0.12 versions. In

Releases(v2.1.0)
  • v2.1.0(Oct 10, 2022)

    Added

    • Support for optimize and optimize:clear commands

    Changed

    • Minimum Laravel Framework version is 9.32

    Full Changelog: https://github.com/allysonsilva/laravel-multienv/compare/v2.0.0...v2.1.0

    Source code(tar.gz)
    Source code(zip)
  • v2.0.0(Apr 13, 2022)

  • v1.0.0(Apr 13, 2022)

Owner
Allyson Silva
๐Ÿš€ Software Architect ๐Ÿ“š๐Ÿง  Php, Go, Laravel, Docker, Kubernetes, MySQL, Redis, MongoDB, TDD, DDD, Design Patterns, SOLID, Clean Code, CI/CD
Allyson Silva
Run multiple websites using the same Laravel installation while keeping tenant specific data separated for fully independent multi-domain setups.

Tenancy for Laravel Enabling awesome Software as a Service with the Laravel framework. This is the successor of hyn/multi-tenant. Feel free to show su

Tenancy 1.1k Dec 30, 2022
Run multiple websites using the same Laravel installation while keeping tenant specific data separated for fully independent multi-domain setups, previously

Run multiple websites using the same Laravel installation while keeping tenant specific data separated for fully independent multi-domain setups, previously

Tenancy 2.4k Jan 3, 2023
A Laravel extension for using a laravel application on a multi domain setting

Laravel Multi Domain An extension for using Laravel in a multi domain setting Description This package allows a single Laravel installation to work wi

null 658 Jan 6, 2023
An example of multi-domain/subdomain app in Laravel.

?? UPDATE A better example with online demo: https://github.com/laravel-101/multi-domain-laravel-app Multi-Domain Laravel App An example of multi-doma

DigitalWheat 204 Dec 27, 2022
Blade Snip allows you to use parts of a blade template multiple times. Basically partials, but inline.

Blade Snip Blade Snip allows you to use parts of a blade template multiple times. Basically partials, but inline: <div class="products"> @snip('pr

Jack Sleight 18 Dec 4, 2022
Taskpm - Run multi tasks by PHP multi process

php-pkg-template Run multi tasks by PHP multi process Install composer composer require phppkg/taskpm Usage github: use the template for quick create

PHPPkg 2 Dec 20, 2021
A simple package that helps PHP developers to generate the QR code signature as per Zakat authority (ZATCA) requirements of Saudi Arabia.

A PHP package that implements the e-invoice QR code signature requirements as designed by the Zakat authority of Saudi Arabia. How to install? compose

Muktar Sayed Saleh 5 Jun 13, 2022
Easy alignment of multiple selections and multi-line selections

Sublime Alignment A simple key-binding for aligning multi-line and multiple selections in Sublime Text 2. Please see http://wbond.net/sublime_packages

Will Bond 516 Dec 28, 2022
Hashtopolis is a multi-platform client-server tool for distributing hashcat tasks to multiple computers.

Hashtopolis is a multi-platform client-server tool for distributing hashcat tasks to multiple computers. The main goals for Hashtopolis's development are portability, robustness, multi-user support, and multiple groups management.

Hashtopolis 1.1k Jan 4, 2023
Load .env files for PHP.

PHP DotEnv Loader Simple library to load and get values from .env file(s). Install composer require murilo-perosa/dot-env How to Use Namespace use Mur

Murilo Perosa 1 Jan 15, 2022