webman
phinx
env
config()
获取配置 (获取失败)<?php
$conn = config('database.default');
$db = config('database.connections.' . $conn);
return [
"paths" => [
"migrations" => "database/migrations",
"seeds" => "database/seeds"
],
"environments" => [
"default_migration_table" => "phinx_migrations",
"default_environment" => "development",
"development" => [
"adapter" => $conn,
"host" => $db['host'],
"name" => $db['database'],
"user" => $db['username'],
"pass" => $db['password'],
"port" => $db['port'],
"charset" => 'utf8mb4',
"collation" => 'utf8mb4_general_ci',
"table_prefix" => $db['prefix'] ?? '',
],
]
];
getenv()
获取配置 (获取失败)<?php
return [
"paths" => [
"migrations" => "database/migrations",
"seeds" => "database/seeds"
],
"environments" => [
"default_migration_table" => "phinx_migrations",
"default_environment" => "development",
"development" => [
"adapter" => 'mysql',
"host" => getenv('DB_HOST'),
"name" => getenv('DB_NAME'),
"user" => getenv('DB_USER'),
"pass" => getenv('DB_PWD'),
"port" => getenv('DB_PORT'),
"charset" => 'utf8mb4',
"collation" => 'utf8mb4_general_ci',
"table_prefix" => getenv('DB_PREFIX') ?? '',
],
]
];
$_ENV
获取配置 (获取成功)在看了env环境组件的仓库 https://github.com/vlucas/phpdotenv 之后,通过readme介绍,用 $_ENV获取成功( $_SERVER也可以,但getenv()仍无法获取 )
<?php
// 加载env的配置
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
return [
"paths" => [
"migrations" => "database/migrations",
"seeds" => "database/seeds"
],
"environments" => [
"default_migration_table" => "phinx_migrations",
"default_environment" => "development",
"development" => [
"adapter" => 'mysql',
"host" => $_ENV['DB_HOST'],
"name" => $_ENV['DB_NAME'],
"user" => $_ENV['DB_USER'],
"pass" => $_ENV['DB_PWD'],
"port" => $_ENV['DB_PORT'],
"charset" => 'utf8mb4',
"collation" => 'utf8mb4_general_ci',
"table_prefix" => $_ENV['DB_PREFIX'] ?? '',
],
]
];
通过3次尝试,总算从环境变量中获取到了配置信息,但总感觉个中不足,因为我更推荐用第一种方法获取,奈何不行。各位大佬有更好的方法请不吝赐教[抱拳][抱拳]
收藏
你的尝试二,有没有装
vlucas/phpdotenv
?php.ini检查 putenv函数是否被禁用
直接用这个 https://github.com/webman-micro/migrations
php webman 查看命令
参考这个文档 https://www.workerman.net/doc/webman/components/env.html
要在 return 前面加上下面这个,意思是吧 当前文件的 .env 文件配置放到 环境变量