将 CI3 升级到 CI4 - 配置文件
Upgrade CI3 to CI4 - configuration files
在我的 Codeigniter 3 中,我有一个简单的 settings.php 文件,看起来像这样:
<?php
$config["lang1_HTML"] = "sr-Latn-RS";
$config["lang1_HTML_2"] = "sr-Latn";
$config["lang1_HTML_3"] = "";
$config["lang1_code"] = "SRP";
$config["lang1_flag"] = "/images/flags/rs.png";
$config["sr"] = "lang1";
$config["lang3"] = "en";
$config["lang3_HTML"] = "en-RS";
$config["lang3_HTML_2"] = "en";
$config["lang3_HTML_3"] = "";
$config["lang3_code"] = "ENG";
...
现在我想把它升级到CI4。有没有机会把这个文件放在 app\Config 中而不改变它并且仍然能够访问这个数组?
或者更好的是可以自动加载 Settings.php 并像这样使用它吗?
首先进入应用配置
public $defaultLocale = 'en';
/**
* --------------------------------------------------------------------------
* Negotiate Locale
* --------------------------------------------------------------------------
*
* If true, the current Request object will automatically determine the
* language to use based on the value of the Accept-Language header.
*
* If false, no automatic detection will be performed.
*
* @var bool
*/
public $negotiateLocale = true;
/**
* --------------------------------------------------------------------------
* Supported Locales
* --------------------------------------------------------------------------
*
* If $negotiateLocale is true, this array lists the locales supported
* by the application in descending order of priority. If no match is
* found, the first locale will be used.
*
* @var string[]
*/
public $supportedLocales = ['en','fa'];
转到应用程序创建文件夹语言
文件夹 en 和文件夹 fa
<?php
en/trans.php
return [
'auth' => [
'validation' => 'information is not valid',
'loggedIn' => 'you are already login',
'notLogIn' => 'you are not login',]];?>
fa/trans.php
<?php
return [
'auth' => [
'validation' => 'خطای اطلاعات وارد شده',
'loggedIn' => 'شما قبل وارد شده بودید',
'notLogIn' => 'شما وارد نشدید',]];?>
读取 header 它发送到 ci4 --
Accept-Language zh
或 Accept-Language fa
echo lang('trans.auth.validation');
Upgrading from 3.x to 4.x » Upgrade Configuration
Upgrade Guide
You have to change the values in the default CI4 config files according to the changes in the CI3 files. The config names are pretty
much the same as in CI3.
If you are using custom config files in your CI3 project you have to create those files as new PHP classes in your CI4 project in
app/Config. These classes should be in the Config
namespace and should extend CodeIgniter\Config\BaseConfig
.
Once you have created all custom config classes, you have to copy the variables from the CI3 config into the new CI4 config class as
public class properties.
Now, you have to change the config fetching syntax everywhere you fetch config values. The CI3 syntax is something like
$this->config->item('item_name');
. You have to change this into
config('MyConfigFile')->item_name;
.
步骤 A:
在扩展 CodeIgniter\Config\BaseConfig
.
的 Config
命名空间中创建一个新的 class app/Config/Settings.php
步骤 B:
将变量从 CI3 配置复制到新的 CI4 配置 class 作为 public class 属性。即:
<?php
namespace Config;
class Settings extends \CodeIgniter\Config\BaseConfig
{
public string $lang1_HTML = "sr-Latn-RS";
public string $lang1_HTML_2 = "sr-Latn";
public string $lang1_HTML_3 = "";
public string $lang1_code = "SRP";
public string $lang1_flag = "/images/flags/rs.png";
public string $sr = "lang1";
// ...
}
步骤 C:
在获取配置值的所有位置更改配置获取语法。
即:从 $this->config->item('lang1_HTML');
到 config(\Config\Settings::class)->lang1_HTML;
。
总结:
CodeIgniter 3.x
CodeIgniter 4.x
1. Loading custom config files.
Manual Loading
$this->config->load('config_filename');
CodeIgniter 4.x will automatically look for the files in all defined namespaces as well as /app/Config/.
2. Dealing with name collisions.
If you need to load multiple config files, normally they will be merged into one master $config
array. To avoid collisions you can set the second parameter to TRUE
and each config file will be stored in an array index corresponding to the name of the config file.
Load config file: $this->config->load('settings', TRUE);
Access item: $this->config->item('settings')['lang1_HTML']
You don't have to worry about this since all config files reside in their own individual classes. Access item: config(\Config\Settings::class)->lang1_HTML
3. Fetching Config items.
$this->config->item('lang1_HTML');
config(\Config\Settings::class)->lang1_HTML
4. Dynamically setting a config item or changing an existing one.
Set item: $this->config->set_item('lang1_HTML', 'sr-Cyrl-ME');
Access set item: $this->config->item('lang1_HTML');
Set item: config(\Config\Settings::class)->lang1_HTML = 'sr-Cyrl-ME'
Access set item: config(\Config\Settings::class)->lang1_HTML
5.Auto-loading.
Global Auto-loading: Open the autoload.php file, located at application/config/autoload.php, and add your config file as indicated in the file.
$autoload['config'] = array('settings');
Global Auto-loading: There is no need to configure this since the config(...) helper function returns a shared instance of the particular config class by default. Hence, you can always access your configs using:
config('config_class_here')->item_name_here;
在我的 Codeigniter 3 中,我有一个简单的 settings.php 文件,看起来像这样:
<?php
$config["lang1_HTML"] = "sr-Latn-RS";
$config["lang1_HTML_2"] = "sr-Latn";
$config["lang1_HTML_3"] = "";
$config["lang1_code"] = "SRP";
$config["lang1_flag"] = "/images/flags/rs.png";
$config["sr"] = "lang1";
$config["lang3"] = "en";
$config["lang3_HTML"] = "en-RS";
$config["lang3_HTML_2"] = "en";
$config["lang3_HTML_3"] = "";
$config["lang3_code"] = "ENG";
...
现在我想把它升级到CI4。有没有机会把这个文件放在 app\Config 中而不改变它并且仍然能够访问这个数组?
或者更好的是可以自动加载 Settings.php 并像这样使用它吗?
首先进入应用配置
public $defaultLocale = 'en';
/**
* --------------------------------------------------------------------------
* Negotiate Locale
* --------------------------------------------------------------------------
*
* If true, the current Request object will automatically determine the
* language to use based on the value of the Accept-Language header.
*
* If false, no automatic detection will be performed.
*
* @var bool
*/
public $negotiateLocale = true;
/**
* --------------------------------------------------------------------------
* Supported Locales
* --------------------------------------------------------------------------
*
* If $negotiateLocale is true, this array lists the locales supported
* by the application in descending order of priority. If no match is
* found, the first locale will be used.
*
* @var string[]
*/
public $supportedLocales = ['en','fa'];
转到应用程序创建文件夹语言 文件夹 en 和文件夹 fa
<?php
en/trans.php
return [
'auth' => [
'validation' => 'information is not valid',
'loggedIn' => 'you are already login',
'notLogIn' => 'you are not login',]];?>
fa/trans.php
<?php
return [
'auth' => [
'validation' => 'خطای اطلاعات وارد شده',
'loggedIn' => 'شما قبل وارد شده بودید',
'notLogIn' => 'شما وارد نشدید',]];?>
读取 header 它发送到 ci4 --
Accept-Language zh 或 Accept-Language fa
echo lang('trans.auth.validation');
Upgrading from 3.x to 4.x » Upgrade Configuration
Upgrade Guide
You have to change the values in the default CI4 config files according to the changes in the CI3 files. The config names are pretty much the same as in CI3.
If you are using custom config files in your CI3 project you have to create those files as new PHP classes in your CI4 project in app/Config. These classes should be in the
Config
namespace and should extendCodeIgniter\Config\BaseConfig
.Once you have created all custom config classes, you have to copy the variables from the CI3 config into the new CI4 config class as public class properties.
Now, you have to change the config fetching syntax everywhere you fetch config values. The CI3 syntax is something like
$this->config->item('item_name');
. You have to change this intoconfig('MyConfigFile')->item_name;
.
步骤 A:
在扩展 CodeIgniter\Config\BaseConfig
.
Config
命名空间中创建一个新的 class app/Config/Settings.php
步骤 B:
将变量从 CI3 配置复制到新的 CI4 配置 class 作为 public class 属性。即:
<?php
namespace Config;
class Settings extends \CodeIgniter\Config\BaseConfig
{
public string $lang1_HTML = "sr-Latn-RS";
public string $lang1_HTML_2 = "sr-Latn";
public string $lang1_HTML_3 = "";
public string $lang1_code = "SRP";
public string $lang1_flag = "/images/flags/rs.png";
public string $sr = "lang1";
// ...
}
步骤 C:
在获取配置值的所有位置更改配置获取语法。
即:从 $this->config->item('lang1_HTML');
到 config(\Config\Settings::class)->lang1_HTML;
。
总结:
CodeIgniter 3.x | CodeIgniter 4.x |
---|---|
1. Loading custom config files. | |
Manual Loading $this->config->load('config_filename'); |
CodeIgniter 4.x will automatically look for the files in all defined namespaces as well as /app/Config/. |
2. Dealing with name collisions. | |
If you need to load multiple config files, normally they will be merged into one master $config array. To avoid collisions you can set the second parameter to TRUE and each config file will be stored in an array index corresponding to the name of the config file. Load config file: $this->config->load('settings', TRUE); Access item: $this->config->item('settings')['lang1_HTML'] |
You don't have to worry about this since all config files reside in their own individual classes. Access item: config(\Config\Settings::class)->lang1_HTML |
3. Fetching Config items. | |
$this->config->item('lang1_HTML'); |
config(\Config\Settings::class)->lang1_HTML |
4. Dynamically setting a config item or changing an existing one. | |
Set item: $this->config->set_item('lang1_HTML', 'sr-Cyrl-ME'); Access set item: $this->config->item('lang1_HTML'); |
Set item: config(\Config\Settings::class)->lang1_HTML = 'sr-Cyrl-ME' Access set item: config(\Config\Settings::class)->lang1_HTML |
5.Auto-loading. | |
Global Auto-loading: Open the autoload.php file, located at application/config/autoload.php, and add your config file as indicated in the file. $autoload['config'] = array('settings'); |
Global Auto-loading: There is no need to configure this since the config(...) helper function returns a shared instance of the particular config class by default. Hence, you can always access your configs using: config('config_class_here')->item_name_here; |