开发者

zend_form: should I use one right in the layout

I usually instantiate my forms in an action and that's where I process them when they're submitted. I then pass them on to the view and output them there as usual.

This form (a search box) is different because it's not part of a single page. It has to be visible everywhere. I've made it part of the template layout.phtml and instantiated and accessed it right there.

$search = new SearchForm(); 
echo $search;

The form prints out fine but the question开发者_如何学JAVA now is where do I handle this form. I usually have processing code like this in the action..

    if ($this->_request->isPost()) {
        //and form is valid
        //process the data            
    }

but since this form is universal, there's no action for it. How should I handle this?

Should I:

  • create a dummy action for it (which doesn't make sense because the form is everywhere)
  • or should I put the processing code right into the layout.phtml (which I think is bad MVC practice because I'm now mixing processing the form with the view).

What should I do? Any advice on this?


Surely the search will need some processing code to build up the results, so I would create this action somewhere generic (like on your IndexController) and point the form at that. Even if the form is on every page it's perfectly fine for you to point it at a specific URL like /search/.

Otherwise you could create a controller plugin that checks the request to see if it has been submitted, and then runs the processing code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜