jbl*_*lue 6 php zend-framework
我有一个在application.ini中定义的值
conditions.time= 50
Run Code Online (Sandbox Code Playgroud)
我怎样才能以zend方式阅读它?
您可以使用Zend_Config_Ini
$config = new Zend_Config_Ini('my/ini/file.ini');
echo $config->conditions->time; // 50
Run Code Online (Sandbox Code Playgroud)
应用程序Bootstrap.php可以访问application.ini使用$this->getOptions(),您可以在注册表中存储您想要的值,如下所示:
public function _initConditions()
{
$config = $this->getOptions();
if (isset($config['conditions']))
{
$registry = Zend_Registry::getInstance();
$registry->conditions = $config['conditions'];
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用注册表访问您的条件,就像您在此处设置它们一样.
这是我的方法,您可以在应用程序的任何位置使用它:
class My_Controller_Action_Helper_Options extends Zend_Controller_Action_Helper_Abstract
{
/**
* Options separator delimiterm e.g.
* option.subkey or
* option/subkey
*/
const DELIMITER = '.';
/**
* Retrieve application options from bootstrap
*
* @return array
*/
public function getOptions()
{
$front = $this->getFrontController();
$bootstrap = $front->getParam('bootstrap');
if (null === $bootstrap) {
throw new Exception('Unable to find bootstrap');
}
return $bootstrap->getOptions();
}
/**
* Get array key if exists, otherwise returns null
*
* @param array $values
* @param string $key
* @return mixed
*/
private static function _getValue($values, $key)
{
if (is_array($values) && isset($values[$key])) {
return $values[$key];
}
return null;
}
/**
* Get application option form bootstrap
*
* @example
* $options = Zend_Controller_Action_HelperBroker::getStaticHelper('options')
* ->get('conditions.time', 'defaultvalue');
*
* @param string $section
* @param mixed $default
* @return Zend_Config
*/
public function get($section = null, $default = null)
{
$value = $this->getOptions();
if (null !== $section && is_string($section)) {
if (false === strpos($section, self::DELIMITER)) {
$value = $this->_getValue($value, $section);
} else {
$sections = explode(self::DELIMITER, $section);
foreach ($sections as $section) {
$value = $this->_getValue($value, $section);
if (null === $value) {
break;
}
}
}
}
if (null === $value) {
return $default;
}
return $value;
}
/**
* @param string $section
* @param mixed $default
* @return Zend_Config
*/
public function direct($section = null, $default = null)
{
return $this->get($section, $default);
}
}
Run Code Online (Sandbox Code Playgroud)