0

从 CodeIgniter 3 升级到 CodeIgniter 4,一开始我发现了一个我无法解决的问题。在 CI3 中,我有一个 switch 语句application/config/config.php来设置$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现在是一个类,我还没有弄清楚如何public $baseURL = "samplefolder";根据ENVIRONMENT变量定义。

立即调用函数不起作用:

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

此外,在声明 with 后调用该函数$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)

我将不胜感激任何帮助!

4

3 回答 3

3

更好的做法是为您的每个环境拥有一个唯一的 .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

于 2020-04-15T06:53:59.593 回答
1

Good question and as I haven't yet looked into doing something similar for my own sites, now is as good as time as any to look into this...

This isn't the greatest of solutions but something to consider.

You could add (append) the following to you App.php to contain...

    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

So changing your CI_ENVIRONMENT Value in your .env file will switch your $baseURL.

A Better way

You might be better off setting your $localFolder as a ENV value so you can control this from the one location.

LOCAL_FOLDER = 'localfolder'

Within your .env file

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

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

Then the setBaseUrl method would become

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;
    }
}

Hoepfully that gives you a few ideas.

于 2020-04-15T03:00:10.797 回答
0

您可以这样做的另一种方法是通过注册商

这种方法的好处是您可以在一个地方覆盖任何配置。您只需创建一个 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']];
    }
}
于 2021-08-08T17:04:59.483 回答