To set/override Environment Variables in Laravel Valet, one had to manually edit the Nginx config files and restart Nginx after doing so. With the release of Laravel Valet 2.1.6 this is no longer needed: Valet 2.1.6 contains a merged PR that provides built-in support for an specific file named .valet-env.php
in which you can set your environment variables.
🕸 Running an older version of Valet? Run these on the CLI to update:
# update package
composer global update
# Make Valet do its housekeeping
valet install
To set environment variables in Valet 2.1.6 or newer, create a file named .valet-env.php
inside the directory where you ran valet link app-name
before. Its contents must return an array with the envvars you want to define.
However, you must group these per app-name
(e.g. the one you used during the valet link
command), or you can use *
as the wildcard. Each app-name
define contains an array in itself, with the keys representing the names of the environment variable, and the values their respective value.
Here’s a few examples:
<?php
return [
'*' => [ // Applies to all
'APP_ENV' => 'dev',
],
];
<?php
return [
'app-name' => [ // Only applies to app-name.test
'APP_ENV' => 'dev',
],
];
It’s possible to combine *
and app-name
, their defined envvars will get merged at runtime:
<?php
return [
'*' => [ // Applies to all
'APP_ENV' => 'dev',
],
'myproject' => [ // Only applies to myproject.test
'DB_NAME' => 'db_devdata',
],
'empty.myproject' => [ // Only applies to empty.myproject.test
'DB_NAME' => 'db_empty',
],
];
Here’s to no more fiddling with ~/.config/valet/Nginx/app-name.test
files 🍻
Thank me with a coffee.
I don\'t do this for profit but a small one-time donation would surely put a smile on my face. Thanks!
To stay in the loop you can follow @bramus or follow @bramusblog on Twitter.
Hey,
thanks for this post.
One thing I don’t get though, why is there even a wildcard option, or the possibility to set project name keys, if the
.valet-env.php
is ONLY read if you drop it into a project’s root…?I have project A: here I drop the env file, and set
But this is then available only for project A anyways, not for project B. Or am I misunderstanding something here?
I’m trying to figure out how (or if) I can set global ENV vars for all projects, so that I don’t have to install a third-party php extension (Maxmind). As 99% of my projects are using these…
The
.valet-env.php
file is meant to be used on a per project-basis, not for all your projects.When you run
valet link projectname
, it will be exposed through the URLhttps://projectname.test/
. Additionally, it is also reachable through any subdomain ofprojectname.test
… and that’s where the support for multiple hostnames comes into play. See the last example in the post:https://myproject.test
, it will use the database that contains dev datahttps://empty.myproject.test
, it will use a database that is empty