application.ini dosyasında tanımlanmış bir değere sahibimApplication.ini'den bir değer okumak
conditions.time= 50
Bir zend eyleminde bunu nasıl okuyabilirim?
application.ini dosyasında tanımlanmış bir değere sahibimApplication.ini'den bir değer okumak
conditions.time= 50
Bir zend eyleminde bunu nasıl okuyabilirim?
Uygulamanızın herhangi bir yerinde kullanabileceğiniz yaklaşımım:
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);
}
}
PHP, bir parse_ini_file() işlevine sahiptir.
Sen, istediğiniz değeri depolayabilir Zend_Config_Ini
$config = new Zend_Config_Ini('my/ini/file.ini');
echo $config->conditions->time; // 50
kullanmak mümkün olabilir kayıt defteriniz şunun gibi:
public function _initConditions()
{
$config = $this->getOptions();
if (isset($config['conditions']))
{
$registry = Zend_Registry::getInstance();
$registry->conditions = $config['conditions'];
}
}
Daha sonra kayıtlarınızı kullanarak, burada ayarladığınız gibi, koşullarınıza erişebilirsiniz.
; application.ini
My.conditions.time= 50
Böyle bir denetleyici içinde kullanabilirsiniz:
Bir önek olarak uygulamanız ad koymak zorundaclass My_Controller_Action_Helper_Config
extends Zend_Controller_Action_Helper_Abstract
{
/**
* @return array
*/
public function direct()
{
$bootstrap = $this->getActionController()->getInvokeArg('bootstrap');
$ns = strtolower(trim($bootstrap->getAppNamespace(), '_'));
return $bootstrap->getOption($ns);
}
}
: Burada
Uygulama en Bootstrap.php
$this->getOptions()
kullanılarak application.ini
erişimi olan kullanabilirsiniz Sen getenv('conditions.time')
Bu, iyi bir yardımcıdır, aksi halde statik olarak çalışmaz. Varsa önyükleyiciyi önyükleyiciden almalısınız. Bir sonraki şey kontrol etmesi gereken, yapılandırma dizisi tuşları var. – takeshin
Bu, yapılandırma dosyasının en az iki kez okunmasına neden olur = önerilmez. Bu değerler zaten bellekte (ön denetleyicinin * önyükleme * paramında) olduğundan, onları buradan almalısınız. – takeshin