简体   繁体   中英

Reading a value from application.ini

I have a value that's defined in application.ini

conditions.time= 50

How can I read it in an zend action the zend way?

You can use Zend_Config_Ini

$config = new Zend_Config_Ini('my/ini/file.ini');
echo $config->conditions->time; // 50

The Application's Bootstrap.php has access to the application.ini using $this->getOptions() , you could store the value you want in your registry something like this:

  public function _initConditions()
  {
    $config = $this->getOptions();

    if (isset($config['conditions']))
    {

      $registry = Zend_Registry::getInstance();

      $registry->conditions = $config['conditions'];

    }
  }

You could then access your conditions using the registry, in much the same way that you set them here.

Here is my approach, which you can use anywhere in the application:

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);
    }
}

Here is an action helper for that :

class 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);
    }
}

You have to put your application namespace as a prefix :

; application.ini
My.conditions.time= 50

You can use it in a controller like this :

$config = $this->_helper->config();
$this->view->time = $config['conditions']['time'];

You might be able to use getenv('conditions.time')

http://www.php.net/manual/en/function.getenv.php

PHP有一个parse_ini_file()函数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM