common constant variables in Zend Framework

2019-06-13 23:48发布

问题:

Where is the best place to create a class that contain all the application constant variables ?
is it : - Bootstrap
- In the Application Common library

for example : 1- When i retrieve an image name from a database if this record doesnt have an image , i want to put a default value somewhere so i can use it in my models

** a constants that i use in all my application so if i change it , i dont want to go back to all in my code and change it everywhere

回答1:

application.ini is the best place for example define some constant there

constants.PUBLIC_PATH =  APPLICATION_PATH "/../public/"
constants.THEME = blue

Then in your bootstrap do

protected function setConstants($constants)
{
    foreach($constants as $name => $value)
    {
         if(!defined($name))
            define($name, $value);
    }

}

ZF take 'constants' from config and call setConstants method in your bootstrap passing all lines prefixed by constants hence its an array .



回答2:

I try not use constants and prefer class constants over global constants. However, when constants are unavoidable, for whatever reason, I go with .ini config file and Bootstrap/library/model class constants.

An example of .ini config constants

(assumes default zf project structure)

application/configs/application.ini

constants.MESSAGE_1 = "Message 1"
constants.MESSAGE_2 = "Message 2"

Bootstap.php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initConstants()
    {
        $options = $this->getOption('constants');

        if (is_array($options)) {
            foreach($options as $key => $value) {
                if(!defined($key)) {
                    define($key, $value);
                }
            }
        }
    }

    // ...
}

Example usage in controller:

class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $this->view->message1 = MESSAGE_1;
        $this->view->message2 = MESSAGE_2;
    }
}

I would extend on the above to allow configuration of how your constants are defined. For example you may want all constants UPPERCASED or not, and allow or disallow already defined constants, so:

application/configs/application.ini

constants.forceUppercase = 1
constants.allowAlreadyDefined = 1
constants.set.message_1 = "Message 1"
constants.set.message_2 = "Message 2"

Bootstrap:

    protected function _initConstants()
    {
        $options = $this->getOption('constants');

        if (isset($options['set']) && is_array($options['set'])) {

            if (isset($options['forceUppercase'])
                && (bool) $options['forceUppercase']) {
                $options['set'] = array_change_key_case($options['set'], CASE_UPPER);
            }

            $allowAlreadyDefined = false;
            if (isset($options['allowAlreadyDefined'])
                && (bool) $options['allowAlreadyDefined']) {
                $allowAlreadyDefined = true;
            }

            foreach($options['set'] as $key => $value) {
                if (!defined($key)) {
                    define($key, $value);
                } elseif (!$allowAlreadyDefined) {
                    throw new InvalidArgumentException(sprintf(
                        "'%s' already defined!", $key));
                }
            }
        }
    }

Bootstrap class constants

Could be your own library, or model class, etc., it depends.

In the bootstrap:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    const MESSAGE_CONSTANT = "Hello World";

    // ...
}

Example usage in controller:

class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $this->view->message = Bootstrap::MESSAGE_CONSTANT;
    }
}