How to modify the. env configuration file of Laravel framework-php Tutorial

Source: Internet
Author: User
Using the laravel framework to develop PHP programs, it is vital to configure the framework's. env file, which requires the configuration of databases, database users, and caches. The following article describes how Laravel can modify the. env configuration file. For more information, see. Using the laravel framework to develop PHP programs, it is vital to configure the framework's. env file, which requires the configuration of databases, database users, and caches. The following article describes how Laravel can modify the. env configuration file. For more information, see.

Preface

Do I have to modify the. env code every time ?. Can the env file be configured in the background? Actually, it is easy to implement. add the following functions to your project.

Sample code

function modifyEnv(array $data) { $envPath = base_path() . DIRECTORY_SEPARATOR . '.env'; $contentArray = collect(file($envPath, FILE_IGNORE_NEW_LINES)); $contentArray->transform(function ($item) use ($data){   foreach ($data as $key => $value){    if(str_contains($item, $key)){     return $key . '=' . $value;    }   }   return $item;  }); $content = implode($contentArray->toArray(), "\n"); \File::put($envPath, $content);}

Use

// You can update any value you want key => value $ data = ['app _ env' => 'Your _ environment ', 'app _ key' => 'Your _ key', 'app _ debug' => 'trueorfalse', 'DB _ database' => 'test ', 'db _ username' => 'test', 'DB _ password' => 'test', 'DB _ host' => 'localhost ', 'cache _ DRIVER '=> 'File', 'session _ DRIVER' => 'File',]; // or $ data = ['DB _ host' => '127. 0.0.1 ',]; // use the function to update modifyEnv ($ data );

Is it easy? in this way, you can configure the Laravel. env file in a graphical way in the background ~

The preceding section describes how to modify the. env configuration file of the Laravel framework. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.