{"id":34004935,"url":"https://github.com/allysonsilva/laravel-multienv","last_synced_at":"2026-04-08T19:31:15.877Z","repository":{"id":57663355,"uuid":"475267972","full_name":"allysonsilva/laravel-multienv","owner":"allysonsilva","description":"A Laravel package that allows you to use multiple \".env\" files in a precedent manner. Use \".env\" files per domain (multi-tentant)!","archived":false,"fork":false,"pushed_at":"2022-10-14T04:49:54.000Z","size":132,"stargazers_count":54,"open_issues_count":1,"forks_count":3,"subscribers_count":3,"default_branch":"2.x","last_synced_at":"2025-12-14T22:29:41.332Z","etag":null,"topics":["environment","environment-variables","laravel","multienv","multitenancy","tenancy"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/allysonsilva.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null}},"created_at":"2022-03-29T03:35:19.000Z","updated_at":"2025-02-02T07:33:14.000Z","dependencies_parsed_at":"2022-09-13T06:50:20.896Z","dependency_job_id":null,"html_url":"https://github.com/allysonsilva/laravel-multienv","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/allysonsilva/laravel-multienv","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allysonsilva%2Flaravel-multienv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allysonsilva%2Flaravel-multienv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allysonsilva%2Flaravel-multienv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allysonsilva%2Flaravel-multienv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/allysonsilva","download_url":"https://codeload.github.com/allysonsilva/laravel-multienv/tar.gz/refs/heads/2.x","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allysonsilva%2Flaravel-multienv/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31571599,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-08T14:31:17.711Z","status":"ssl_error","status_checked_at":"2026-04-08T14:31:17.202Z","response_time":54,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["environment","environment-variables","laravel","multienv","multitenancy","tenancy"],"created_at":"2025-12-13T10:20:48.747Z","updated_at":"2026-04-08T19:31:15.864Z","avatar_url":"https://github.com/allysonsilva.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Multi ENVs\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"/images/art/socialcard.png\" alt=\"Social Card of Laravel Multi ENVs\"\u003e\n\u003c/p\u003e\n\n[![PHP Version][ico-php]][link-php]\n[![Laravel Version][ico-laravel]][link-laravel]\n[![CI Status][ico-actions]][link-actions]\n[![PHPCS - GitHub Workflow Status](https://github.com/allysonsilva/laravel-multienv/actions/workflows/phpcs.yml/badge.svg)](https://github.com/allysonsilva/laravel-multienv/actions/workflows/phpcs.yml)\n[![PHPMD - GitHub Workflow Status](https://github.com/allysonsilva/laravel-multienv/actions/workflows/phpmd.yml/badge.svg)](https://github.com/allysonsilva/laravel-multienv/actions/workflows/phpmd.yml)\n[![PHPStan - GitHub Workflow Status](https://github.com/allysonsilva/laravel-multienv/actions/workflows/phpstan.yml/badge.svg)](https://github.com/allysonsilva/laravel-multienv/actions/workflows/phpstan.yml)\n[![Coverage Status][ico-codecov]][link-codecov]\n[![Latest Version][ico-version]][link-packagist]\n[![MIT Licensed](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE.md)\n\n\u003e 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*).\n\n## 🚀  Installation\n\n### Requirements\n\nThe package has been developed and tested to work with the following minimum requirements:\n\n- *PHP 8.0*\n- *Laravel 9.0*\n\n### Laravel version Compatibility\n\n| Laravel | PHP |    Package   |\n|:-------:|:---:|:------------:|\n|    9.x  | 8.0 |   **^2.0**   |\n|    8.x  | 7.4 |   **^1.0**   |\n\n### Install the Package\n\nYou can install the package via Composer:\n\n```bash\ncomposer require allysonsilva/laravel-multienv\n```\n\n### Publish the Config\n\nYou can then publish the package's config file by using the following command:\n\n```bash\nphp artisan vendor:publish --tag=\"envs-config\"\n```\n\n## 🔧  Configuration\n\n1. Add trait to **kernel console** `app/Console/Kernel.php`:\n    ```diff\n    \u003c?php\n\n    namespace App\\Console;\n\n    use Illuminate\\Console\\Scheduling\\Schedule;\n    +use Allyson\\MultiEnv\\Concerns\\ConsoleCallTrait;\n    +use Allyson\\MultiEnv\\Concerns\\BootstrappersTrait;\n    use Illuminate\\Foundation\\Console\\Kernel as ConsoleKernel;\n\n    class Kernel extends ConsoleKernel\n    {\n    +    use BootstrappersTrait, ConsoleCallTrait;\n    ```\n\n2. Add trait to **kernel http** `app/Http/Kernel.php`:\n    ```diff\n    \u003c?php\n\n    namespace App\\Http;\n\n    +use Allyson\\MultiEnv\\Concerns\\BootstrappersTrait;\n    use Illuminate\\Foundation\\Http\\Kernel as HttpKernel;\n\n    class Kernel extends HttpKernel\n    {\n    +    use BootstrappersTrait;\n    ```\n\n## 📖  Usage\n\n\u003e **The use of this package happens by manipulating the `.envs` files in the project.**\n\n### Using multiple `.env` files in the **project root**\n\n- *You can use as many `.env` files as you like.*\n\n- 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.\n  - 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.*\n\n- Use the `config('envs.ignored')` regex to ignore `.env` files that should not be processed/handled.\n\n#### See it in action\n\nAssuming we have 3 `.env` files in the root of the application with their *environment variables* as follows:\n\n```diff\n.\n├── app\n├── bootstrap\n├── config\n├── database\n├── envs\n│   ├── .env.site1.test\n│   └── .env.site2.test\n├── lang\n├── public\n├── resources\n├── routes\n├── storage\n├── tests\n├── .env\n├── .env.example\n+├── .envA\n+├── .envB\n+├── .envC\n├── .gitattributes\n├── .gitignore\n├── README.md\n├── artisan\n├── composer.json\n└── phpunit.xml\n```\n\n*Obs: The [`envs`](#using-multiple-env-files-per-domain) folder in the application structure below will be explained later.*\n\nEach `.env` file has its environment variables set as follows:\n\n**`.envA`:**\n\n```\nENV_NAME=\"NAME Env A\"\nAPP_URL=http://env-a.test\n\nENV_FILE_A=.envA\n```\n\n**`.envB`:**\n\n```\nENV_NAME=\"NAME Env B\"\nAPP_URL=http://env-b.test\n\nENV_FILE_B=.envB\n```\n\n**`.envC`:**\n\n```\nENV_NAME=\"NAME Env C\"\nAPP_URL=http://env-c.test\n\nENV_FILE_C=.envC\n```\n\nAs 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:\n\n```\nENV_NAME=\"NAME Env C\"\nAPP_URL=http://env-c.test\n\nENV_FILE_A=.envA\nENV_FILE_B=.envB\nENV_FILE_C=.envC\n```\n\nUsing the configuration of `config('envs.sorted')`, you can customize the default order of file priorities:\n\n```php\n'sorted' =\u003e [\n    '.envA',\n    '.envC',\n    '.envB',\n],\n```\n\nAs above, the result of the *environment variables* of the 3 files would be:\n\n```\nENV_NAME=\"NAME Env B\"\nAPP_URL=http://env-b.test\n\nENV_FILE_A=.envA\nENV_FILE_C=.envC\nENV_FILE_B=.envB\n```\n\n### Using multiple `.env` files **per domain**\n\nIn the same way that you use [multiple envs files in the root of the application](#using-multiple-env-files-in-the-project-root), it is possible to use `.env` files per domain:\n\n- 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.\n\n- In order for the `.env` file to match the domain / subdomain, it must be created as follows: `.env.\u003cdomain\u003e`.\n\n- 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.\n\n#### See it in action\n\nUsing 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*.\n\nAssuming that the `.env.site1.test` file has the following variables:\n\n```\nENV_NAME=\"NAME Env SITE 1\"\nAPP_URL=http://site1.test\n```\n\nWhen *environment variables* are used in the project, they will have the following results:\n\n```\nENV_NAME=\"NAME Env SITE 1\"\nAPP_URL=http://site1.test\n\nENV_FILE_A=.envA\nENV_FILE_C=.envC\nENV_FILE_B=.envB\n```\n\nTo see the examples in action, let's use [this laravel application](https://github.com/allysonsilva/laravel-multienv-use).\n\n#### Domain custom `.env` file name\n\nWhen the domain's `.env` filename is different from the default which is: `.env.\u003cdomain\u003e`, then set the `env` key in the domain configs in `config('envs.domains') `, as follows:\n\n```php\n'domains' =\u003e [\n    'your-domain.tld' =\u003e [\n        'env' =\u003e '.env.custom-name',\n    ],\n],\n```\n\n### Using `config:cache` and `route:cache` **per domain**\n\n**You can cache configs and routes by domain.**\n\n#### Caching *configs* by domain - `config:cache`\n\nA 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.\n\nUse 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.\n\nTo generate and use the `.php` file with a custom name of the domain cache settings, use the following code as an example:\n\n```php\n'domains' =\u003e [\n    'site2.test' =\u003e [\n        'APP_CONFIG_CACHE' =\u003e 'config-site2-test.php',\n    ],\n],\n```\n\n#### Caching *routes* by domain - `route:cache`\n\nAs 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.\n\nIt 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:\n\n```php\n'domains' =\u003e [\n    'site2.test' =\u003e [\n        'APP_ROUTES_CACHE' =\u003e 'routes-v7-site2-test.php',\n    ],\n],\n```\n\n## 🧪  Testing\n\n``` bash\ncomposer test:unit\n```\n\n## 📝  Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information about the changes on this package.\n\n## 🤝  Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n## 🔒  Security\n\nIf you discover any security related issues, please email github@allyson.dev instead of using the issue tracker.\n\n## 🏆  Credits\n\n- [Allyson Silva](https://github.com/allysonsilva)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n\n[ico-php]: https://img.shields.io/packagist/php-v/allysonsilva/laravel-multienv?color=%234F5B93\u0026logo=php\n[ico-laravel]: https://img.shields.io/static/v1?label=laravel\u0026message=%E2%89%A59.0\u0026color=ff2d20\u0026logo=laravel\n[ico-actions]: https://github.com/allysonsilva/laravel-multienv/actions/workflows/ci.yml/badge.svg\n[ico-codecov]: https://codecov.io/gh/allysonsilva/laravel-multienv/branch/main/graph/badge.svg?token=H546OKODQB\n[ico-version]: https://img.shields.io/packagist/v/allysonsilva/laravel-multienv.svg?label=stable\n\n[link-php]: https://www.php.net\n[link-laravel]: https://laravel.com\n[link-actions]: https://github.com/allysonsilva/laravel-multienv/actions/workflows/ci.yml\n[link-codecov]: https://codecov.io/gh/allysonsilva/laravel-multienv\n[link-packagist]: https://packagist.org/packages/allysonsilva/laravel-multienv\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fallysonsilva%2Flaravel-multienv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fallysonsilva%2Flaravel-multienv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fallysonsilva%2Flaravel-multienv/lists"}