views:

58

answers:

2

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  A: 

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)

Rahul Singhania
+3  A: 

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.

David Weinraub
I think the factory ideia is great, with some kind of name convention, the user can simply put the form in the directory and the factory class will load than.
Keyne
I wanted to use the autoloader like you describe, but it's a pretty complex beast and I couldn't figure out how to load resources before the default zend stuff. I think I'll make a separate question for that. edit: http://stackoverflow.com/questions/3373891/how-to-configure-the-zend-autoloader-to-load-a-custom-path-for-resources-before-t
Peter