Zend, Global variable in Application.ini?

2019-03-16 08:10发布

I have a question as I need to have a global static variable and I have question is any possibility to add it to the application.ini file, how to do it?

Or I have to:

  1. Create abstract class with static variable,
  2. Register it in Zend_Registry to have access to this variable from all application (Register it in Bootstrap file)
  3. I can use it, or there is easier, I mean "automatic way" to do it?

Thanks for advice! Regars,

6条回答
看我几分像从前
2楼-- · 2019-03-16 08:45

I just added define('VARIABLE_NAME', 'Something here'); to bootstrap. Instant global constants.

These constants were placed inside of a protected _intiFuncName function inside of the bootstrap extends class

查看更多
贼婆χ
3楼-- · 2019-03-16 08:57

in Application.ini file

someservice.apikey  = 12345678
someservice.passkey = 87654321

in bootstrap

public function _initSomeservice()
{
    $someservice = $this->getOption('someservice');
    Zend_Registry::set('someservice', $someservice);
}

to pull from Registry

$config = Zend_Registry::get('someservice');
查看更多
叼着烟拽天下
4楼-- · 2019-03-16 09:06

You can specify value in the application.ini and in the Bootsrap.php you read it and save in Zend_Registry. You can also use PHP define method to create constants (if you veriable is constant).

查看更多
冷血范
5楼-- · 2019-03-16 09:07

The best solution for me was to create a variable in index.php file. I have added: defined('VARIABLE_NAME') || define('VARIABLE_NAME', 'Something here');

And now I can access to it from everywhere ;D Thanks a lot!

查看更多
何必那么认真
6楼-- · 2019-03-16 09:07

I wouldn't use the Zend_Registry nor constants, for two reasons

  1. In the registry you are never sure what happened with the variables. Every part of the application can change them and you wouldn't be noticed. I see the registry actually as an anit-pattern.
  2. Constants are polluting your application unnecessarily. What if you use for every configuration some constant? You have to keep an additional document with all the constants defined, it's horrible to maintain.

What the best option is, is to directly get the config object from the bootstrap. In next examples, I assume you have this in your application.ini:

someservice.apikey  = 12345678
someservice.passkey = 87654321

The bootstrap is set as a parameter in the frontController. If you have an action controller, this makes it as simple as this:

$serviceOptions = $this->getInvokeArg('bootstrap')->getOption('someservice');

If you have a service instantiated in your controller, you now can pass them on through the constructor and/or setter.

If you want to get the options not within your controller, but somewhere else, you can use the singleton pattern the frontController implements. So at any place (of course only during dispatching, not during the bootstrap itself) you're able to do this:

$frontController = Zend_Controller_Front::getInstance();
$serviceOptions  = $frontController->getParam('bootstrap')
                                   ->getOption('someservice');

With above method you're safe you have always the right configuration option and not some possibly mutated one.

查看更多
Summer. ? 凉城
7楼-- · 2019-03-16 09:08

I have a series of constants with the following naming convention in application.ini:

system.image_path  = "/assets/images"
system.custom_path = "/assets/custom"
system.cdn_URL     = "http://cdnurl.com"  

And I just reference them in my code as if they were PHP constants:

SYSTEM_IMAGE_PATH
SYSTEM_CUSTOM_PATH
SYSTEM_CDN_URL
查看更多
登录 后发表回答