在Zend Framework PHP中在页面之间持久化数据时使用会话变量与请求参数
我试图更好地理解在这种情况下,使用Zend Framework在请求之间持久化数据的最佳方法是什么: 假设我有一个事件控制器,默认索引视图显示任何现有公告(如果有),以及添加新公告的链接事件和公告都是任意对象。我正在尝试检索eventId,以便在将新公告保存到数据库时将其与之关联。从组成上看,一个事件由0到多个公告组成。从我对Zend框架的有限理解中,我看到了两个主要的选择 选项一:使URL类似于“/event/addAnnouncement/eventId/5”,这使得通过路由/路径参数检索eventId变得容易 选项二:在控制器的indexAction中,将eventId保存到会话变量,然后可以在事件控制器的addAnnouncementAction中检索该变量。这样,添加公告链接将只是“/event/addAnnouncement/” 有人能解释一下这两种方法中哪一种更好,或者我不知道还有其他方法吗在Zend Framework PHP中在页面之间持久化数据时使用会话变量与请求参数,php,zend-framework,session-state,Php,Zend Framework,Session State,我试图更好地理解在这种情况下,使用Zend Framework在请求之间持久化数据的最佳方法是什么: 假设我有一个事件控制器,默认索引视图显示任何现有公告(如果有),以及添加新公告的链接事件和公告都是任意对象。我正在尝试检索eventId,以便在将新公告保存到数据库时将其与之关联。从组成上看,一个事件由0到多个公告组成。从我对Zend框架的有限理解中,我看到了两个主要的选择 选项一:使URL类似于“/event/addAnnouncement/eventId/5”,这使得通过路由/路径参数检索e
一如既往,我们非常感谢您的帮助。谢谢。选项1更好,尽管在您的示例中,这不是一篇文章,但可以通过一篇文章来完成 选项2的问题是: 如果一个用户同时打开多个窗口或选项卡,与不同的事件相关,您将如何跟踪应该使用哪个事件ID? 如果用户将“添加事件”页面标记为书签并稍后返回,则可能不会设置会话变量 选项2的实现也有点复杂,并且增加了对会话的依赖。要问自己的问题是,数据需要保留多长时间?如果只需要保存数据以将其传递给下一个可以使用POST或GET的操作,则GET将通过url传递,而POST通常不会 您给出的示例建议您需要将数据保持足够长的时间,以验证、过滤和处理数据。因此,您可能会非常满意地将少量数据作为参数POST或GET传递。这将提供您需要的临时持久性,并且还提供了一个额外的好处,即一旦发出未传递变量的请求,数据就会过期 一个简单的示例假设表单使用POST方法传递数据: 如果您需要将数据持久化更长的时间,则$\u会话可能会派上用场。在ZF中,您通常使用Zend_会话_名称空间来操作会话数据。 使用Zend_会话_名称空间很容易,下面是一个我经常使用它的示例
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_会话_名称空间非常相似,通常用于在引导中保存配置数据,但可以存储几乎所有需要存储的内容,并且也被许多内部Zend类使用,最显著的是
我希望这有帮助。如果您有更多问题,请查看以下链接:
当用户点击后退按钮/event/addAnnouncement/eventId/5时,使用post everywhere将使他们的生活变得非常有趣:这称为路径,与HTTP方法无关。GetParams simply set返回请求对象中设置的参数,这些参数可以来自示例中的路由变量,获取或发布数据。如果使用AJAX通过获取/发布请求创建公告,该怎么办。这样您就不必离开事件对象的页面了?@KingCrunch在ZF/event/addAnnouncement/eventId/5中的功能与get请求相同,大致等于?controller=event&action=addanunction&eventId=5
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
}
}
}
//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;
}