Using the Laravel framework to develop PHP programs, configuring the. env file for the framework is critical, and the file requires a configuration database, database users, and caching. The following article mainly introduces you how to modify Laravel. env configuration file related information, you need to refer to the friend.
Objective
Does env have to be changed by changing the code? Env files can be configured in the background? In fact, it is very simple to implement, add the following function 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::p ut ($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 ' = ' Localh ' Ost ', ' cache_driver ' = ' file ', ' session_driver ' = ' file ',];//or $data = [' db_host ' = ' 127.0.0.1 ',];//use function update m Odifyenv ($data);
How, is not very simple, so that you can configure the Laravel. env file through the background graphics ~