How to dynamically override forms and/or views usi

2019-09-02 06:56发布

问题:

I'm working on a web application using Zend that we'd like to ship with some default forms and views. We'd like the client to be able to create custom forms and/or views that could be placed in another directory that could override the default forms. IE, Zend would check to see if any custom forms (or views) existed, and if so, load those, otherwise load the default ones.

Is this possible to do using Zend?

I already thought about creating subclasses for every form that by default would just call the parent constructor, but that seems like a bit of a hack.

回答1:

How about a factory/service class, configured with your custom and default prefixes/paths, whose job is to instantiate the requested form. It would be the job of this factory class to check first for the presence of a custom form and to fallback to the default form.

So you could have default forms named something like My_Form_Default_User appearing in application/forms/default and your default forms named something like My_Form_Custom_User appearing in application/forms/custom. Then client code could call something like My_FormFactory::createForm('user').

An alternative might be to name the forms the same thing (Ex: My_Form_User) for both cases, but then configure the autoloader with paths to both locations. If the autoloader finds the custom form, cool. If not, it falls back to the default location. But that approach seems a bit dicier to me.



回答2:

How about this?

Have a section in your application.ini which will be read before rendering the view. The default setting would indicate to your controller that it must load YOUR form but if the client wants to load their own form for a given view then they can simply delete the corresponding entry from your application.ini (you might well have to provide a custom application.ini which can be customized by your client but which is different from the application.ini used by your own application)