【问题标题】:How to update the file .env with a form symfony4如何使用表单 symfony4 更新文件 .env
【发布时间】:2019-04-19 11:27:51
【问题描述】:

我想用表单更新文件 .env,但是当我写入文件 .env 并测试是否可以连接时,它不起作用。

谢谢。

它是主控制器

...

$form = $this->createForm(ConfigurationType::class);            
            if($request->isMethod('POST')){                             
                $form->handleRequest($request);             
                if($form->isSubmitted() && $form->isValid()){                   
                    $data_form = $form->getData();
                    unset($form);               
                    $form = $this->createForm(ConfigurationType::class);                    

                    $new_conf2 = "DATABASE_URL=mysql://".$data_form['username'].":".$data_form['password']."@".$data_form['adresse']."/";
                    $lines = file('../.env');                                   
                    $temp = "";

                    foreach($lines as $line){                       
                        if(strstr($line, "DATABASE_URL")){
                            echo "C'est la bonne.";
                        }else{
                            echo "Non";
                            $temp .= $line;
                        }
                    }

                    $temp2 = $temp;

                    try{                        
                        $nom_bd = "symfony_test";                           
                        $temp2 .= $new_conf2;
                        file_put_contents('../.env');                                                   
                        $em = $this->getDoctrine()->getManager();
                        $em->getConnection()->connect();                            
                        $connected = $em->getConnection()->isConnected();                                           
                        $sql ="CREATE DATABASE ".$nom_db;                           
                        $stmt = $em->getConnection()->prepare($sql);                            
                        $result = $stmt->execute();                             
                        return $this->redirectToRoute('index');                                                                             
                    }catch(\Exception $e){
                        echo "Erreur pas de connexion".$e->getMessage();                            
                    }                   
                }

...

【问题讨论】:

    标签: php mysql config symfony4


    【解决方案1】:

    如果这是您的实际代码,我认为问题在于您只为 file_put_contents() 提供了一个参数。它至少需要两个。第二个是要写入文件的数据。

                    try{                        
                        $nom_bd = "symfony_test";                           
                        $temp2 .= $new_conf2;
    
                        file_put_contents('../.env', $theDataToBeWritten );// <--
    
                        $em = $this->getDoctrine()->getManager();
                        $em->getConnection()->connect();                            
                        $connected = $em->getConnection()->isConnected();                                           
                        $sql ="CREATE DATABASE ".$nom_db;                           
                        $stmt = $em->getConnection()->prepare($sql);                            
                        $result = $stmt->execute();                             
                        return $this->redirectToRoute('index');                                                                             
                    }catch(\Exception $e){
                        echo "Erreur pas de connexion".$e->getMessage();                            
                    }              
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-11
      • 1970-01-01
      • 1970-01-01
      • 2018-10-30
      • 2018-06-16
      • 1970-01-01
      • 2021-09-12
      相关资源
      最近更新 更多