【问题标题】:common constant variables in Zend FrameworkZend 框架中的常用常量变量
【发布时间】:2012-05-02 06:48:30
【问题描述】:

哪里是创建包含所有应用程序常量变量的类的最佳位置?
是吗 : - 引导
- 在应用程序公共库中

例如: 1-当我从数据库中检索图像名称时,如果此记录没有图像,我想在某处放置一个默认值,以便我可以在我的模型中使用它

** 我在所有应用程序中使用的常量,所以如果我更改它,我不想回到我的代码中的所有内容并在任何地方更改它

【问题讨论】:

  • 您能解释一下为什么要使用它们吗?例如,为什么您更喜欢使用常量而不是将配置对象存储在注册表中?
  • 好的,我现在就编辑我的问题
  • 我认为使用全局常量有点违背常见的设计模式和现代编码。我认为您最好在 Zend_Registry 的全局范围内使用 Zend_Config 对象,或者从您的模型中填充这些默认图像值,这在这种情况下看起来是正确的解决方案。

标签: zend-framework


【解决方案1】:

application.ini 是最好的地方,例如在那里定义一些常量

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

然后在你的引导中做

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

}

ZF 从配置中获取“常量”并在引导程序中调用 setConstants 方法,传递所有以常量为前缀的行,因此它是一个数组。

【讨论】:

  • 如何从我的控制器调用它们?
  • 您调用它们的方式与调用使用 define() 方法创建的任何常量的方式相同,使用 PUBLIC_PATH 和 THEME,包括控制器在内的任何地方。
【解决方案2】:

我尽量不使用常量,我更喜欢类常量而不是全局常量。但是,当常量不可避免时,无论出于何种原因,我都会使用 .ini 配置文件和 Bootstrap/library/model 类常量。

.ini 配置常量示例

(假设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);
                }
            }
        }
    }

    // ...
}

控制器中的示例用法:

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

我会在上面进行扩展以允许配置如何你的常量是定义的。例如,您可能希望所有常量都大写或不大写,并允许或禁止已定义的常量,因此:

application/configs/application.ini

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

引导程序:

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

引导类常量

可以是你自己的库,也可以是模型类等,视情况而定。

在引导程序中:

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

    // ...
}

控制器中的示例用法:

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

【讨论】:

    猜你喜欢
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多