I need to set some data to the env.php file from my controller.Is there any possible way to do so.
-
1What kind of data do you want to add ? Any reason why it has to be this file in particular ?Raphael at Digital Pianism– Raphael at Digital Pianism2016-10-07 10:10:54 +00:00Commented Oct 7, 2016 at 10:10
-
I was trying to add some data like server, port etc to the env file.user43415– user434152016-10-07 10:39:37 +00:00Commented Oct 7, 2016 at 10:39
Add a comment
|
1 Answer
\Magento\Framework\App\DeploymentConfig\Writer class is responsible for write deployment configuration. You can ask it in the constructor of your controller and then save data.
Example code:
<?php
class MyAction extends \Magento\Framework\App\Action\Action
{
public function __construct(
Context $context,
\Magento\Framework\App\DeploymentConfig\Writer $deploymentConfig
) {
parent::__construct($context);
$this->deploymentConfig = $deploymentConfig;
}
public function execute()
{
echo $this->deploymentConfig->saveConfig([ConfigFilePool::APP_ENV => ['your_key' => $data]]);
die();
}
}
-
Thanks for your response. But how can we write data to the env file programmaticallyuser43415– user434152016-10-07 12:09:27 +00:00Commented Oct 7, 2016 at 12:09
-
-
Thank you very much for your reply.It was really helpful for me..!user43415– user434152016-10-12 04:28:41 +00:00Commented Oct 12, 2016 at 4:28
-
To use saveConfig() func we need to inject Magento\Framework\App\DeploymentConfig\Writer;user43415– user434152016-10-12 04:30:49 +00:00Commented Oct 12, 2016 at 4:30
-
eg :-$this->writer->saveConfig([ConfigFilePool::APP_ENV => ['your_key' => $data]]);user43415– user434152016-10-12 04:32:05 +00:00Commented Oct 12, 2016 at 4:32