我试图更好地了解在这种情况下在请求之间持久化数据的最佳方法是什么(使用 Zend Framework(:
假设我有一个事件控制器,默认(索引(视图显示任何现有的公告(如果有(,以及指向添加新公告的链接(事件和公告都是任意对象(。我正在尝试检索 eventId,以便在将新公告保存到数据库时将其与它相关联。从组合上讲,事件由 0 到多个公告组成。从我对 Zend 框架的有限理解来看,我看到了两个主要选项。
选项一:将 URL 设置为类似于"/event/addAnnouncement/eventId/5",这使得通过路由/路径参数检索 eventId 变得容易。
选项二:在控制器的 indexAction 中,将 eventId 保存到会话变量,然后可以在事件控制器的 addNoticeAction 中检索该变量。这样,"添加公告"链接将只是"/事件/添加公告/"。
谁能阐明这两种方法中哪一种更好,或者是否有我不知道的另一种方法?
与往常一样,非常感谢任何帮助。谢谢。
要问自己的问题是,您需要将数据保留多长时间?
如果您只需要保存数据以将其传递给下一个操作,则可以使用 POST 或 GET,则 GET 将通过 url 传递,而 POST 不会(通常(.
/>
您提供的示例表明,您需要将数据保留足够长的时间以验证、筛选和处理数据。因此,您可能会非常满意将几条数据作为参数(POST 或 GET(传递。这将提供您需要的临时持久性,并提供额外的好处,即一旦发出未传递变量的请求,数据就会过期.
一个简单的示例(假设您的表单使用 POST 方法传递数据(:
if ($this->getRequest()->isPost()) {
if ($form->isValid($this->getRequest()->getPost()){
$data = $form->getValues();//filtered values from form
$model = new Appliction_Model_DbTable_MyTable();
$model->save($data);
//but you need to pass the users name from the form to another action
//there are many tools in ZF to do this with, this is just one example
return $this->getHelper('Redirector')->gotoSimple(
'action' => 'newaction',
array('name' => $data['name'])//passed data
);
}
}
如果您需要将数据保留更长的时间,那么 _SESSION 美元可能会派上用场。在 ZF 中,您通常使用 Zend_Session_Namespace()
来操作会话数据。它很容易使用Zend_Session_Namespace
,这是我经常使用它的示例。
class IndexController extends Zend_Controller_Action {
protected $_session;
public function init() {
//assign the session to the property and give the namespace a name.
$this->_session = new Zend_Session_Namespace('User');
}
public function indexAction() {
//using the previous example
$form = new Application_Form_MyForm();
if ($this->getRequest()->isPost()) {
if ($form->isValid($this->getRequest()->getPost()){
$data = $form->getValues();//filtered values from form
//this time we'll add the data to the session
$this->_session->userName = $data['user'];//assign a string to the session
//we can also assign all of the form data to one session variable as an array or object
$this->_session->formData = $data;
return $this->getHelper('Redirector')->gotoSimple('action'=>'next');
}
}
$this->view->form = $form;
}
public function nextAction() {
//retrieve session variables and assign them to the view for demonstration
$this->view->userData = $this->_session->formData;//an array of values from previous actions form
$this->view->userName = $this->_session->userName;//a string value
}
}
}
您需要在应用程序中保留的任何数据都可以发送到任何操作、控制器或模块。请记住,如果您重新提交该表单,则保存到这些特定会话变量的信息将被覆盖.
ZF 中还有一个选项介于传递参数和在会话中存储数据之间,Zend_Registry
.它的用法与Zend_Session_Namespace
非常相似,通常用于将配置数据保存在引导程序中(但几乎可以存储您需要存储的任何内容(,并且还被许多内部Zend类使用,尤其是flashmessenger操作助手。
//Bootstrap.php
protected function _initRegistry() {
//make application.ini configuration available in registry
$config = new Zend_Config($this->getOptions());
//set data in registry
Zend_Registry::set('config', $config);
}
protected function _initView() {
//Initialize view
$view = new Zend_View();
//get data from registry
$view->doctype(Zend_Registry::get('config')->resources->view->doctype);
//...truncated...
//Return it, so that it can be stored by the bootstrap
return $view;
}
我希望这有所帮助。如果您有更多问题,请查看这些链接:
采埃孚请求对象
Zend_Session_NamespaceZend_Registry
选项 1 更好,尽管在您的示例中这不是 POST(但可以使用 POST 来完成(。
选项 2 的问题是:
- 如果用户同时打开了多个与不同事件相关的窗口或选项卡,您将如何跟踪应使用哪个事件 ID?
- 如果用户为添加事件页面添加了书签,但稍后返回,则可能无法设置会话变量
选项 2 的实现也稍微复杂一些,并增加了对会话的依赖。