zend-framework - Zend 框架 : Resource Plugins vs Methods

标签 zend-framework

我在看 Zend Framework Book: Survive the Deep End关于资源方法。它讲述了资源方法将如何覆盖资源插件。

But wait, there's also a Resource Plugin (Zend_Application_Resource_View) which may also create a Resource called View. Can we have two View Resources? The answer is no - we can have one and one only



Zend Framework Manual ,

a good way to create re-usable bootstrap resources and to offload much of your coding to discrete classes is to utilize resource plugins ... the intention is that developers should write their own to encapsulate their own initialization needs



对我来说,资源方法似乎是一种更直观的资源初始化方式,那我为什么要使用插件呢?这只是我更喜欢哪个的问题吗?还是在不同的情况下使用?

资源方法会替换或添加提供的资源插件提供的功能吗?因为如果它被替换,我需要确保我初始化 全部 变量或我需要的任何东西?

By returning the new Zend_View instance from _initView(), Zend_Application will accept the replacement and will not attempt to overwrite our changes by running Zend_Application_Resource_View to set up a standard default Zend_View instance with the flaws we just corrected



如果我不返回 Zend_View ,就好像我没有方法一样?我可以说我应该总是从资源方法中返回一些东西吗?

Here, we do the same thing by using the getResource() method to retrieve an instance of Zend_Controller_Front created and configured by Zend_Application_Resource_Frontcontroller



从上面可以说,如果我希望我的资源方法具有由提供的资源插件设置的默认值,我可以做一个 getResource()第一?

最佳答案

回答您的问题:

我应该使用资源插件还是方法?

我会说这主要取决于个人喜好。正如您从手册中引用的那样,如果您使用资源插件,则在另一个项目中重用代码会变得更容易(因为移动/测试类比从方法中剪切和粘贴文本更容易) .在我看来,方法可以更容易地查看 bootstrap 中发生的事情,至少在它们开始变得有点复杂之前,在这种情况下,将它们转换为插件是有意义的。

资源方法会替换或添加资源插件提供的功能吗?

我相信它的工作方式是在首次实例化引导类时加载和初始化插件。然后 bootstrap 将通过您的方法并运行这些方法。如果您有一个与插件资源名称相同的方法,您的方法将覆盖该插件。但是,您也可以从您的方法访问现有资源并对其进行修改,在这种情况下,您的方法将添加到插件提供的功能中。

请记住,插件不会自己神奇地运行(除了将始终运行的前端 Controller 插件)。它们只会在您的 application.ini 触发它们时使用(或者如果您从自己的方法中调用它们)。

如果我不返回 Zend_View,就好像我没有方法一样?我可以说我应该总是从资源方法中返回一些东西吗?

从资源方法返回一些东西是一种很好的做法,因为这允许其他方法或应用程序的其他部分访问该资源。但是,该方法仍将在没有返回值的情况下运行。但是如果你添加了一个 _initView 方法并设置了一个新的 Zend_View 对象,如果你不做任何事情,它不会对你的应用程序产生任何影响。

我可以说如果我希望我的资源方法具有由提供的资源插件设置的默认值,我可以执行 getResource()

是的。但我会确保在这种情况下返回资源,以便访问资源的任何其他方法都使用您修改过的方法,而不是插件设置的方法。

我个人要么坚持使用 application.ini + 资源插件,要么坚持使用资源方法。如果所有资源都在一个地方,就更容易看到发生了什么。

关于zend-framework - Zend 框架 : Resource Plugins vs Methods,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3275598/

相关文章:

php - 将类别从父可配置产品复制到 Magento 中的简单子(monad)产品

zend-framework - 如何在 xampp 中安装 Zend 框架 1.12.9?

PHPExcel 集成到 Zend 框架中

php - Zend Framework,在没有 View 的情况下运行查询?

php - 无法使用多个文件输入字段。曾德

zend-framework - Zend 框架 : Hide navigation item in menu by show in breadcrumbs

PHP - $request->getPost ('first_name' )

zend-framework - 将 CSS 文件添加到布局 Zend 框架

zend-framework - Zend_Controller_Router_Exception : Route default is not defined

mysql - zend paginator 随机排序