简体   繁体   English

如何使用表单symfony4更新文件.env

[英]How to update the file .env with a form symfony4

i would like update the file .env with a form but when i write in the file .env and i test if i can connect, it doesn't work. 我想用一个表单更新文件.env但是当我在文件中写入.env并且我测试我是否可以连接,它不起作用。

thanks. 谢谢。

it's the maincontroller 它是主控制器

... ...

$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();                            
                    }                   
                }

... ...

If this is your actual code, I think the problem is that you are only providing one argument to file_put_contents(). 如果这是您的实际代码,我认为问题是您只向file_put_contents()提供一个参数。 It requires at least two. 它至少需要两个。 The second is the data to be written to the file. 第二个是要写入文件的数据。

                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();                            
                }              

Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM