Zend Framework: Resource Plugins and Methods

I read the Zend Framework Book: Survive the Deep End about resource methods. he talks about how resource methods will override the resource plugin.

But wait, there is also a Plugin resource (Zend_Application_Resource_View), which can also create a resource called View. Can we have two kinds of resources? The answer is no, we can only do one

in the Zend Framework Guide ,

A good way to create reusable boot resources and offload most of your coding is discrete classes - use resource plugins ... the intention is that developers have to write their own to encapsulate their own initialization needs

for me, resource methods look like a more intuitive way to initialize resources, why then should I use plugins? Is this the question I prefer? or are they used in different circumstances?

Will the resource methods replace or add the functionality of the provided resource plugins? because if it replaces, will I need to make sure that I initialize all variables or all I need?

By returning a new Zend_View instance from _initView (), Zend_Application will accept the replacements and will not try to overwrite our changes by running Zend_Application_Resource_View to install the standard standard Zend_View instance with flaws we just fixed

If I do not return Zend_View , it will be as if I did not have a method? can I say that I should always return some of the resource methods?

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

from the above, can I say that if I want my resource methods to have the default values ​​set by the provided resource plugin, can I do getResource() 1st?

+4
source share
1 answer

Answering your questions:

Should I use resource plugins or methods?

I would say that this largely depends on personal preferences. As the quote from the manual says, if you use the resource plugin, it becomes easier to reuse the code in another project (since it’s easier to move / test the class than to cut the text from the method). In my opinion, the methods make it a little easier to understand what is happening in bootstrap, at least until they become a little complicated, in which case it would be advisable to convert them into a plugin.

Will the resource method be replaced or added to the functions provided by the resource plugins?

I believe that it works, so that plugins are loaded and initialized when the bootstrap class is started first. Then the bootstrap will go through your methods and run them. If you have a method with the same name as the plugin resource, your method will override this plugin. However, you can also access an existing resource from your method and modify it, in which case your method adds the functionality provided by the plugin.

Remember that plugins do not magically run on their own (except for the front controller plugin, which will always work). They will only be used if your application.ini starts them (or you call them from your own methods).

If I do not return Zend_View, it will be as if I did not have a method? can I say that I should always return some of the resource methods?

It’s good practice to return one of the resource methods, as this allows you to access the resource in other ways or in other parts of the application. However, the method will still work without a return value. But if you add the _initView method and set a new Zend_View object, if you do nothing with it, it will not affect your application.

May I say that if I want my resource methods to have the default values ​​set by the provided resource plugin, I can do getResource ()

Yes. But I would make sure that you return the resource in this case, just so that any other methods that access the resource use your changed, and not just one setting using the plugin.

Personally, I would either stick with application.ini + resource plugins, or resource methods. It’s easier to understand what happens if the entire resource is in one place.

+5
source

Source: https://habr.com/ru/post/1316074/


All Articles