I wanna get app/etc/env.php variables in Magento 2 inside php code.
how is it possible?
for example I wanna something like this:
<?php
echo $_ENV['db']['host'];
?>
I mean I need to get the host that had been set in the env.php file.
I wanna get app/etc/env.php variables in Magento 2 inside php code.
how is it possible?
for example I wanna something like this:
<?php
echo $_ENV['db']['host'];
?>
I mean I need to get the host that had been set in the env.php file.
2
Answers
You have to declared the values for that env param first.
You can do it in the *.php file like that:
If your’re using apache declare env in .htaccess like that:
If you’re using nginx you need to use fastcgi_params and set
variables_order = "EGPCS"
in your php.ini file.Found the answer here: Magento 2. Retrieve information from env.php programmatically
Basically you want to inject
MagentoFrameworkAppDeploymentConfig
and then use itsget()
method:GitHub Source
For example:
$deploymentConfig->get('db/connection/default/host');
Additional: env.php reference