如何在 CodeIgniter 4 中基于 ENVIRONMENT 定义 baseURL

How to define baseURL based on ENVIRONMENT in CodeIgniter 4

从 CodeIgniter 3 升级到 CodeIgniter 4,一开始我发现了一个我无法解决的问题。 在 CI3 中,我在 application/config/config.php 中有一个 switch 语句来设置 $config['base_url']。 就像

$localFolder = "localfolder";

switch (ENVIRONMENT) {
    case 'development':
        $config['base_url'] = "http://$localFolder.loc";
        break;

    default:
        $config['base_url'] = "http://testserver.com/$localFolder";
        break;
}

但在 CI4 中,app/Config/App.php 现在是 class,我还没有弄清楚如何根据 ENVIRONMENT 变量定义 public $baseURL = "samplefolder";

立即调用函数无效:

public $baseURL = (function(){
        switch (ENVIRONMENT) {
            case 'development':
                $this->baseURL = "http://$localFolder.loc";
                break;

            default:
                $this->baseURL = "http://testserver.com/$localFolder";
                break;
        }
    })();

错误:

Fatal error: Constant expression contains invalid operations

此外,在使用 $this-> 声明后调用函数会产生错误:

public $baseURL = "";

public function baseURL($localFolder)
    {
        switch (ENVIRONMENT) {
            case 'development':
                $this->baseURL = "http://$localFolder.loc";
                break;

            default:
                $this->baseURL = "http://testserver.com/$localFolder";
                break;
        }
    }

$this->baseURL("localfolder");

错误:

Parse error: syntax error, unexpected '$this' (T_VARIABLE), expecting function (T_FUNCTION) or const (T_CONST)

如有任何帮助,我将不胜感激!

好问题,因为我还没有研究过为我自己的网站做类似的事情,现在正是研究这个问题的最佳时机...

这不是最好的解决方案,但值得考虑。

您可以添加(追加)以下内容 App.php 以包含...

    protected $localFolder = 'localfolder';

    public function __construct() {
        parent::__construct();

        $this->setBaseUrl(); // Set the Base URL
    }

    protected function setBaseUrl() {
        switch ($_ENV['CI_ENVIRONMENT']) {
            case 'development':
                $this->baseURL = "http://$this->localFolder.loc";
                break;
            default:
                $this->baseURL = "http://testserver.com/$this->localFolder";
                break;
        }
    }
} // End of APP Class

因此,更改 .env 文件中的 CI_ENVIRONMENT 值将切换 $baseURL。

更好的方法

您最好将 $localFolder 设置为 ENV 值,这样您就可以从一个位置控制它。

LOCAL_FOLDER = 'localfolder'

在您的 .env 文件中

#--------------------------------------------------------------------
# ENVIRONMENT
#--------------------------------------------------------------------

# CI_ENVIRONMENT = production
CI_ENVIRONMENT = development
LOCAL_FOLDER = 'localfolder'

那么setBaseUrl方法会变成

protected function setBaseUrl() {
    switch ($_ENV['CI_ENVIRONMENT']) {
        case 'development':
            $this->baseURL = "http://{$_ENV['LOCAL_FOLDER']}.loc";
                break;
        default:
            $this->baseURL = "http://testserver.com/{$_ENV['LOCAL_FOLDER']}";
                break;
    }
}

希望能给你一些想法。

更好的做法是为每个环境创建一个唯一的 .env 文件。然后将您的 conf 设置到那些 .env 文件中。 例如,您可以有一个这样的 .env 文件:

#--------------------------------------------------------------------
# ENVIRONMENT
#--------------------------------------------------------------------

CI_ENVIRONMENT = development

#--------------------------------------------------------------------
# APP
#--------------------------------------------------------------------

app.baseURL = 'http://localfolder.loc'

另一个 app.baseURL = 'http://testserver.com/localfolder'

去查看有关它的 CI4 文档:https://codeigniter.com/user_guide/general/configuration.html#handling-different-environments

另一种方法是通过 Registrars

这种方法的好处是您可以在一个地方覆盖任何配置。您只需创建一个 Config/Registrar.php 文件,然后像这样覆盖配置:

namespace Config;

class Registrar
{
    public static function App(): array {
        return [
            'production' => ['baseURL' => 'https://example.com'],
            'development' => ['baseURL' => 'https://dev.example.com']
        ][$_ENV['CI_ENVIRONMENT']];
    }
}