webman如何设置env来切换环境(开发or生产)?
1、在文件support/helper.php
添加:
function env($key, $default = null)
{
static $env_config = [];
if (!$env_config) {
$env_config = include base_path() . DIRECTORY_SEPARATOR . '.env.php';
}
return $env_config[$key] ?? $default;
}
2、在项目根目录添加.env.php
:
<?php
// config/app.php
$app = [
'APP_NAME' => 'webman',
'APP_ENV' => 'dev',
'APP_DEBUG' => true,
];
// config/database.php
$mysql = [
'DB_HOST' => '127.0.0.1',
'DB_PORT' => 3306,
'DB_USER' => 'root',
'DB_PASSWORD' => 'root',
];
//合并数组
return array_merge($app, $mysql);
3、在config/database.php
中改为:
return [
// 默认数据库
'default' => 'mysql',
// 各种数据库配置
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env("DB_HOST", '127.0.0.1'),
'port' => env("DB_PORT", 3306),
'database' => 'test',
'username' => env('DB_USER', 'root'),
'password' => env('DB_PASSWORD', 'root'),
'unix_socket' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
注意:.env.php 不提交到版本库(.gitignore里加一行.env.php)。
本文转自:https://www.workerman.net/q/7564