如何在 Yii 上使用自定义配置文件
How To Use Custom Config File On Yii
我正在使用 Yii Framework 并且我在配置文件夹中有自定义 customconfig.php 文件
我想使用我的 customconfig :
<?php return array('var' => 'variable'); ?>
我怎样才能得到我的自定义变量?
通过 params
存储在 main.php 配置文件中的数组索引,这种传递变量到配置是可能的。
假设您有两个配置文件:main.php 和 customconfig.php。在 main.php 你有基础配置,例如组件、模块等。它是 Yii 中使用的标准配置文件。第二个文件,customconfig.php 是带有自定义变量的自定义文件。
首先,您需要创建 customconfig.php 文件,其中包含 returns 数组 params
索引:
<?php
//this is customconfig.php config file
return array(
'params'=>array('variableName'=>'variableValue'),
);
下一步是将您的 customconfig.php 文件内容与包含来自 main.php 文件的配置的数组合并:
<?php
//this is main.php config file
return CMap::mergeArray(
require(dirname(__FILE__).'/customconfig.php'), //here is path to custom config file
array(
//your config data
'params'=>array(
// ...
),
)
);
现在您可以使用 Yii::app()->params['variableName']
.
访问 customconfig.php 中的变量
我正在使用 Yii Framework 并且我在配置文件夹中有自定义 customconfig.php 文件
我想使用我的 customconfig :
<?php return array('var' => 'variable'); ?>
我怎样才能得到我的自定义变量?
通过 params
存储在 main.php 配置文件中的数组索引,这种传递变量到配置是可能的。
假设您有两个配置文件:main.php 和 customconfig.php。在 main.php 你有基础配置,例如组件、模块等。它是 Yii 中使用的标准配置文件。第二个文件,customconfig.php 是带有自定义变量的自定义文件。
首先,您需要创建 customconfig.php 文件,其中包含 returns 数组 params
索引:
<?php
//this is customconfig.php config file
return array(
'params'=>array('variableName'=>'variableValue'),
);
下一步是将您的 customconfig.php 文件内容与包含来自 main.php 文件的配置的数组合并:
<?php
//this is main.php config file
return CMap::mergeArray(
require(dirname(__FILE__).'/customconfig.php'), //here is path to custom config file
array(
//your config data
'params'=>array(
// ...
),
)
);
现在您可以使用 Yii::app()->params['variableName']
.