Home > Backend Development > PHP Tutorial > Laravel 友好的修改 .env 配置文件

Laravel 友好的修改 .env 配置文件

WBOY
Release: 2016-06-23 13:27:04
Original
1002 people have browsed it

每次改 .env 都得通过修改代码来改吗?.env 文件能不能通过在后台配置呢?
其实是很简单就可以实现的,把以下函数加入到你的项目中

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);}
Copy after login

使用

// 你可以更新你想要的任何值 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',]; // 或者$data = [    'DB_HOST' => '127.0.0.1',];// 使用函数更新modifyEnv($data);
Copy after login

怎样,是不是很简单,这样就可以通过后台图形化配置 Laravel 的 .env 文件啦~

欢迎加群交流学习:365969825

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template