05.
dotenv: Environmental Variables
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
11 Comments
Another useful tool - https://github.com/dotenv-l....
It’s a lightning-fast linter for .env files. Written in Rust.
Hey @mgrachev,
Thanks for sharing it with others. It might be useful for someone, or might be an overkill, depends on the project.
Cheers!
Do we need to use "vlucas/phpdotenv" or "symffony/dotenv" ? what's is the diffrence any way Is it possible to access to environments into a syfmony command ? I'm facing this error In EnvVarProcessor.php line 76 and I'm using symfony/dotenv
[Symfony\Component\DependencyInjection\Exception\EnvNotFoundException]
Environment variable not found
withh symfony3.4
Hey ahmedbhs!
First, this IS a bit of an old tutorial, as modern Symfony handles all of this for you :). But, the questions are still interesting! So, let's see:
It basically doesn't matter. Symfony made their version specifically to be a bit less fancy and more shell-like. And then more features were added.
Definitely! There are a few ways, the easiest (but maybe not cleanest) would be to grab it form $_SERVER - like
$_SERVER['MY_VAR']. A more advanced way would be to inject it into your commands by leveraging the dependency injection container. And... it looks like maybe you are trying this?Can you show me your code? This would usually happen if you're configuring a service and using the
%env(SOME_VAR)%format to access an environment var (which is good!). But, for some reason your environment variable isn't found. The logic that class uses is pretty simple - you can see it here: https://github.com/symfony/symfony/blob/4eb10eaa2ce831880ed5f433ac418cadf64f5128/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php#L70-L82Share some code related to this if you can and I'm sure we can get it working :). Btw, using symfony/dotenv vs vlucas for this specific piece should not make a difference.
Cheers!
What is the difference? Which environment var used by Symfony 4.x?
SYMFONY_DEBUG vs APP_DEBUG and APP_ENV vs SYMFONY_ENV?
Yo Csaba B.!
Ah, great question! In practice, very little :). This tutorial pre-dates Symfony 4 and Symfony Flex by quite a long time. But some of its ideas were eventually used in Symfony 4, and this is one of them. In this tutorial, SYMFONY_ENV becomes the Symfony environment and in Symfony 4, APP_ENV becomes the Symfony environment. There are some subtle differences, but basically they do the same thing. The same is true for the DEBUG env vars, except that you don't *need* to define APP_DEBUG in Symfony 4 - if you don't define it, it's "guessed" from the APP_ENV value.
Cheers!
Thank you so much :)
I found some useful info too about env variables on Fabien's blog site. See the "Environment Variables" part here: http://fabien.potencier.org...
Yes, great resource! Thanks for sharing!
Hello
Anyone knows how to define the databse url ? Been trying out https://medium.com/@fabpot/...
but it doesn't seem to like the url my mysql also uses a password unlike the example.
Tried like this DATABASE_URL=mysql://newuser:Dorobanti71#$@127.0.0.1:3306/symfony?charset=utf8mb4
but am getting
DBALException in DriverManager.php line 259:
Malformed parameter "url".
in DriverManager.php line 259
at DriverManager::parseDatabaseUrl(array('url' => 'mysql://newuser:Dorobanti71#$@127.0.0.1:3306/symfony?charset=utf8mb4', 'host' => 'localhost', 'port' => null, 'user' => 'root', 'password' => null, 'driver' => 'pdo_mysql', 'driverOptions' => array(), 'defaultTableOptions' => array())) in DriverManager.php line 144
hmm weird it works with root user without password
seems the parseDatabaseUrl function https://github.com/doctrine... doesn't like that url string.
Yo Caim Astraea!
I don't know the answer specifically, but I do know that the DriverManager you linked to uses
parse_urlto break the URL into pieces. So that should, in theory, work fine. However, you might need to urlencode some of the special characters in your password. Check out the latest commit to that class, which add support for this: https://github.com/doctrine/dbal/commit/2dc419625299be3b91ced60ca4658fb95abb3722, and the PR behind it: https://github.com/doctrine/dbal/pull/2504Cheers!
"Houston: no signs of life"
Start the conversation!