zend framework display/process form using same action/view?
Using MVC architecture (Zend Framework), should you use a single view with if statements to display/process a form, or multiple views (i.e. one view to display form, one to display result).
In this instance, I am trying to produce a google like search engine. The layout of the page will fundamentally change when displaying search results.
For example; Controller:
public function indexAction()
{
if (!$this->getRequest()->isPost()) {
// display form
} else {
if ($this->_request->isPost()) {
if (!$form->isValid($formData)) {
开发者_运维知识库 // re-display form with errors
} else {
// process form and;
// display result using same action/view?
// display result using same action but use a different view?
}
}
}
}
Using the same view:
<?php if(isset($this->form)) : ?>
<!-- show form -->
<?php else: ?>
<!-- show result -->
<?php endif; ?>
Hope that makes sense.
Don't get caught up in the old monolithic style of coding where one hunk of code does everything for one output page. The whole point of MVC is so separate responsibility. Think of a view as a template for a single piece of output, regardless of what page/url it appears on. The form is one piece of output. The search results is another. Use two views.
Less if-s -- less bugs. I propose you to use more different views here, that can reuse one form.
精彩评论