user285594
user285594

Reputation:

ZendFramework - How do i declare static global constant, to have this available in my all controllers and models?

I have 50 controllers, and 90 Models. Everywhere i was using/repeating same words 'login' and 'success'. I want to make one global constant so that i can access from anywhere in my controllers and models. How or where do you declare this following 2 line.

Try to put this somewhere to have global access:

defined('MYFIXED_WORD_SUCCESS')
    || define('MYFIXED_WORD_SUCCESS', "success");
defined('MYFIXED_WORD_LOGIN')
    || define('MYFIXED_WORD_SUCCESS', "login");

Example of repeating:

if (count($result) > 0) 
{
  $return = array(
        'flag'  => 'login',  // replace it with MYFIXED_WORD_
        'result'=> 'success',// replace it with MYFIXED_WORD_
        'extra' => array(group'=>$rec->group,));

Upvotes: 2

Views: 2555

Answers (3)

nevvermind
nevvermind

Reputation: 3392

I'm using a constants.ini file in application/configs folder, and, in Bootstrap.php, I'm iterating and defining constants:

/**
 * Loads app-wide constants from ini file
 */
protected function _initDefineConstants()
{
    $constantFile = APPLICATION_PATH . '/configs/constants.ini';
    $iniParser = new Zend_Config_Ini($constantFile);

    foreach ($iniParser->toArray() as $constName => $constantVal) {
        define($constName, $constantVal);
    }
}

Example of constants.ini:

ADMIN_PRODUCTS_PER_PAGE = 20
PRODUCT_VIEW_REC = 4 ; number of recommended products in Product view

; columns in "options" table
OPT_COL_VAT = vat
OPT_TEL_ORDER = telephone_order
OPT_QUICK_EMAIL = quick_order_email

Upvotes: 1

turist_ua
turist_ua

Reputation: 114

You can save your variables in Zend_Registry

Upvotes: 2

Iznogood
Iznogood

Reputation: 12843

Anything you include or set in bootstrap.php should be available from anywhere in your application. But have a look at Zend_Translate since it does pretty much what you want but in a organised way. Alternatively you could set it in application.ini and get to it thru Zend_Config.

You "could" put it in /public/index.php but in my opinion its not the place to put such thing. Imagine when you have thousands of const like that how it would polute a file that basically never should change.

Upvotes: 1

Related Questions