1.Zend_Config概述
Zend_Config
被设计在应用程序中简化访问和使用配置数据。配置文件可以是.ini格式,xml格式,也可以是一个.php。在需要用到的地方就可以通过
new Zend_Config('配置文件路径')调用配置文件。比如数据库的连接。
2.我的代码
例子1:我把config.php文件放在application里面。
<?php
return array(
'webhost' => 'www.example.com',
'database' => array(
'adapter' => 'pdo_mysql',
'params' => array(
'host' => 'db.example.com',
'username' => 'dbuser',
'password' => 'secret',
'dbname' => 'mydatabase'
)
)
);
?>
然后随便在一个index.phtml中测试
<pre name="code" class="php"><?php
$config = new Zend_Config(require '../application/configs/config.php');
// Print a configuration datum (results in 'www.example.com')
echo $config->database->adapter;
?>
例子2:zend_config_ini
<pre name="code" class="php"><pre name="code" class="php">; 生产站点配置数据
[production]
webhost = www.example.com
database.adapter = pdo_mysql
database.params.host = db.example.com
database.params.username = dbuser
database.params.password = secret
database.params.dbname = dbname
; 开发站点配置数据从生产站点配置数据集成并如果需要可以重写
[staging : production]
database.params.host = dev.example.com
database.params.username = devuser
database.params.password = devsecret
$config = new Zend_Config_Ini('/path/to/config.ini', 'staging');
echo $config->database->params->host; // 输出 "dev.example.com"
echo $config->database->params->dbname; // 输出 "dbname"
例子3:zend_config_xml
<pre name="code" class="php">
</pre><pre name="code" class="html"><?xml version="1.0"?>
<configdata>
<production>
<webhost>www.example.com</webhost>
<database>
<adapter>pdo_mysql</adapter>
<params>
<host>db.example.com</host>
<username>dbuser</username>
<password>secret</password>
<dbname>dbname</dbname>
</params>
</database>
</production>
<staging extends="production">
<database>
<params>
<host>dev.example.com</host>
<username>devuser</username>
<password>devsecret</password>
</params>
</database>
</staging>
</configdata>
$config = new Zend_Config_Xml('/path/to/config.xml', 'staging');
echo $config->database->params->host; // 输出 "dev.example.com"
echo $config->database->params->dbname; // 输出 "dbname"