我是Zend Framework的新手,并且遇到了一个似乎很简单但很难修复的问题(至少对我而言)。
我有一个带索引和列表操作的控制器。在索引操作中,我正在收集用户输入以构建查询,准备Db Select查询并使用会话命名空间来保存它。在列表操作中,从会话命名空间中检索查询并以网格形式显示。
问题是在浏览器中复制网页会保留旧会话。解决这个问题的最佳方法是什么?我知道以这种方式使用会话并不是一个好习惯,但无法找到替代方案。
这是我想要实现的目标:
类QueryController扩展了Zend_Controller_Action {
public function indexAction()
{
$form = new Application_Form_Query();
$form->submit->setLabel('Search');
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
// here ideally I would like to redirect to listAction with $formData
// but don't know how to achieve that,
// with _forward it displays both form and the grid on the same page
// this tip was provided by ArneRie (thanks ArneRie)
}
public function listAction()
{
// I want this action to be called everytime after form input and
// cycle without going back to form (indexAction)
// get the $formData from indexAction() or reposted in listAction()
// build query based on $formData
// display grid
// re-post data back to request object
}
}
注意:欢迎任何其他建议,即带示例的最佳做法。
答案 0 :(得分:0)
我假设在索引操作中您有表单,那么为什么不将表单的操作指向列表控制器以在那里验证,准备和查询?
告诉我们您想要做什么,也许我们可以找到更好的解决方案。
编辑21/12/2010
class QueryController extends Zend_Controller_Action{
public function indexAction()
{
$form = new Application_Form_Query();
$form->submit->setLabel('Search');
$form->setMethod('post')->setAction('/query/list');
$this->view->form = $form;
}
public function listAction()
{
$session = new Zend_Session_Namespace('queryssesion');
if (!isset($session->query)) { $session->query = $YOUR_QUERY }
$form = new Application_Form_Query();
if ($this->getRequest()->isPost() && $form->isValid($_POST))
{
$formData = $this->getRequest()->getPost();
$session->query = $formData->query //example
}
}
}
这个会话的事情应该有用,如果不是我认为我们可以尝试使用ajax并将查询作为POST发送;]