phinx.php获取.env环境变量中的配置

lee23

webman phinx env

开发过程中,按webman文档推荐的数据迁移工具phinx,进行了安装。安装完成后,不想把根目录下的配置文件phinx.php写死,于是乎进行了一些尝试:

尝试一:用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次尝试,总算从环境变量中获取到了配置信息,但总感觉个中不足,因为我更推荐用第一种方法获取,奈何不行。各位大佬有更好的方法请不吝赐教[抱拳][抱拳]

2156 5 3
5个评论

mincon

收藏

  • 暂无评论
tzhack

你的尝试二,有没有装vlucas/phpdotenv

composer require vlucas/phpdotenv
  • 暂无评论
etree

php.ini检查 putenv函数是否被禁用

  • 暂无评论
weijer

直接用这个 https://github.com/webman-micro/migrations

截图

php webman 查看命令

要在 return 前面加上下面这个,意思是吧 当前文件的 .env 文件配置放到 环境变量

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
  • 暂无评论

lee23

320
积分
0
获赞数
0
粉丝数
2023-04-01 加入
×
🔝