Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to set some data to the env.php file from my controller.Is there any possible way to do so.

Thanks in advance

share|improve this question
1  
What kind of data do you want to add ? Any reason why it has to be this file in particular ? – Raphael at Digital Pianism Oct 7 at 10:10
    
I was trying to add some data like server, port etc to the env file. – Rahul Oct 7 at 10:39
up vote 0 down vote accepted

\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();
    }
}
share|improve this answer
    
Thanks for your response. But how can we write data to the env file programmatically – Rahul Oct 7 at 12:09
    
I update Answer. – KAndy Oct 7 at 12:24
    
Thank you very much for your reply.It was really helpful for me..! – Rahul Oct 12 at 4:28
    
To use saveConfig() func we need to inject Magento\Framework\App\DeploymentConfig\Writer; – Rahul Oct 12 at 4:30
    
eg :-$this->writer->saveConfig([ConfigFilePool::APP_ENV => ['your_key' => $data]]); – Rahul Oct 12 at 4:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.