Media website professional
ZendFramework, Php & Mysql, Jquery, javascript, Css, Html...
Showing posts with label
Zend_Config
.
Show all posts
Showing posts with label
Zend_Config
.
Show all posts
Read and write configuration files the format xml with Zend_Config
1. Read ini file.
I have a configuration file /application/configs/db.ini as follows:
www.example.com
pdo_mysql
localhost
root
root
zftraining
www.example.com
pdo_mysql
localhost
vanlanh
123456
zftraining
We use the class to read file Zend_Config_Xml db.xml
public function xmlAction(){ $xml = CONFIG_PATH . '/db.xml'; $section = 'data'; $config = new Zend_Config_Xml($xml, $section); echo '
' . $config->webhost; echo '
' . $config->database->adapter; echo '
' . $config->database->params->username; echo '
'; echo '
'; print_r($config->toArray()); echo '
'; }
Output:
www.example.com pdo_mysql vanlanh Array ( [webhost] => www.example.com [database] => Array ( [adapter] => pdo_mysql [params] => Array ( [host] => localhost [username] => vanlanh [password] => 123456 [dbname] => zftraining ) ) )
2.Write xml file
public function writeXmlAction(){ $data = array( 'global'=>array( 'smtp'=>array( 'parameters'=>array( 'host' => "smtp.example.com", 'port' => "587", 'username' => "jeky11", 'password' => "hyd3", 'security' => "tls", ) ) ), 'data'=>array( 'version' => "1.1" ) ); $config = new Zend_Config_Writer_Xml(); $filename = CONFIG_PATH . '/my01.xml'; $objConfig = new Zend_Config($data); $config->write($filename,$objConfig); }
My01.xml output file:
smtp.example.com
587
jeky11
hyd3
tls
1.1
Read and write configuration files the format ini with Zend_Config
1. Read ini file.
I have a configuration file /application/configs/db.ini as follows:
[information] webhost =www.example.com database.adapter =pdo_mysql database.params.host =localhost database.params.username=root database.params.password=root database.params.dbname =zftraining [data:information] webhost =www.example.com database.adapter =pdo_mysql database.params.host =localhost database.params.username=root1 database.params.password=root database.params.dbname =zftraining
We use the class to read file Zend_Config_Ini db.ini
public function indexAction(){ $filename = CONFIG_PATH . '/db.ini'; $section = 'infomation'; $config = new Zend_Config_Ini($filename, $section); echo '<pre>'; print_r($config->toArray()); echo '</pre>'; echo '<br>' . $config->webhost; echo '<br>' . $config->database->adapter; echo '<br>' . $config->database->params->username; echo '<br><hr>'; }
Output:
Array ( [webhost] => www.example.com [database] => Array ( [adapter] => pdo_mysql [params] => Array ( [host] => localhost [username] => root [password] => root [dbname] => zftraining ) ) ) www.example.com pdo_mysql root
2.Write ini file
public function writeIniAction(){ $data = array( 'global'=>array( 'smtp'=>array( 'parameters'=>array( 'host' => "smtp.example.com", 'port' => "587", 'username' => "jeky11", 'password' => "hyd3", 'security' => "tls", ) ) ), 'data'=>array( 'version' => "1.1" ) ); $config = new Zend_Config_Writer_Ini(); $config->setNestSeparator('::'); $filename = CONFIG_PATH . '/my01.ini'; $objConfig = new Zend_Config($data); $config->write($filename,$objConfig); }
My01.ini output file:
[global] smtp::parameters::host = "smtp.example.com" smtp::parameters::port = "587" smtp::parameters::username = "jeky11" smtp::parameters::password = "hyd3" smtp::parameters::security = "tls" [data] version = "1.1"
Older Posts
Home
Subscribe to:
Posts (Atom)