访问 Zend_Application_Bootstrap_Bootstrap 的 getOptions()

发布于 2025-01-06 11:53:56 字数 472 浏览 2 评论 0原文

我使用默认的 Zend_Application 设计模式,该模式会在应用程序引导程序中自动加载 zend config ini 文件,并且我需要跨多个模型和控制器加载 ini 文件的变量。

现在,我通过将配置对象设置为 Zend_Registry 的键来解决这个问题:

protected function _initConfig()
{
    $config = new Zend_Config($this->getOptions());
    Zend_Registry::set('config', $config);
}

一般来说,我不喜欢使用 Zend_Registry,因为它不提供代码在我的 IDE 中自动完成,并且很难跟踪注册表命名空间中的内容。

还有另一种方法可以访问 Zend_Application 的配置 ini 吗?

I'm using the default Zend_Application design pattern which loads a zend config ini file automatically in the application bootstrap and I need to ini file's variables across many models and controllers.

Right now, I'm solving it by settings the config object as a key into Zend_Registry:

protected function _initConfig()
{
    $config = new Zend_Config($this->getOptions());
    Zend_Registry::set('config', $config);
}

Generally, I don't like using Zend_Registry, as it doesn't offer code auto complete in my IDE, and it's hard to keep track on what I have in the registry namespace.

Is there another way to access the Zend_Application's config ini?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海夕 2025-01-13 11:53:56

在控制器中,您应该能够执行以下操作:

$this->getInvokeArg('bootstrap')->getOptions();

访问配置。对于模型,您确实应该传递您需要的选项。否则你唯一的选择就是注册表。

In a controller you should be able to do:

$this->getInvokeArg('bootstrap')->getOptions();

to access the config. For models you really should be passing in the options you need. Otherwise your only choice is really the registry.

勿忘初心 2025-01-13 11:53:56

您始终可以根据需要自己使用

$options = new Zend_Config_Ini('/path/to/config.ini',
                               'config');

Wich 初始化它,这几乎就是引导程序为您所做的事情。然后你就会在 $options 上自动完成。但每次需要时都必须对其进行初始化。
我认为修改代码以适应自动完成功能并不是最好的主意。但这是个人的。

如果我没有弄错 Zend Studio 8/9(也许是 7),即使对于 Zend_Registry::get() 返回的对象,您也确实具有自动完成功能。

You could always initialise it as needed yourself with

$options = new Zend_Config_Ini('/path/to/config.ini',
                               'config');

Wich is pretty much what the bootstrap does for you. Then you would have autocomplete on $options. But you would have to initialise it everytime you need it.
I think modifying your code to suit autocomplete is not the greatest idea ever. But this is personnal.

If I am not mistaken with Zend Studio 8/9 (maybe 7) you DO have autocomplete even for objects returned by Zend_Registry::get().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文