在Zend Framework中处理会话处理的最佳方式 [英] Best way to deal with session handling in Zend Framework
问题描述
所以我在Zend框架中启动,并希望实现一个站点范围的用户会话....我可以很容易地从应用程序中的所有模块/控制器访问。
So I'm starting up in Zend framework and looking to implement a site-wide "User" session.... something I can easily access from ALL modules/controllers in the application.
我想,我应该在库中创建一个新的命名空间并扩展控制器,如:
I'm like, should I make a new namespace in the library and extend the controller, like:
class MYCUSTOMLIB_Controller_Action extends Zend_Controller_Action
{
protected $_userSession;
function preDispatch(Zend_Controller_Request_Abstract $req)
{
$this->_userSession = new Zend_Session_Namespace('user');
}
}
然后让我所有的控制器/模块/ etc从那里?
ANd then have all my controllers/modules/etc extend from that?
或者我应该创建一个插件或什么?你将如何使这个插件将用户会话传递给控制器?
Or should I create a Plugin or what? How would you go about making this plugin to pass the user session to the controller?
或者我在引导?再次,如何传递给控制器?
Or do I do it in the bootstrap?? Again how to pass to controller?
还应该使用Zend_Session_Namespace或Zend_Http_Cookie,以及如何加密和xss清理cookie或自动完成?
Also should I use Zend_Session_Namespace or Zend_Http_Cookie and also how do I encrypt and xss clean the cookie or is that done automagically?
推荐答案
我也会在bootstrap中初始化:
I would initialise in the bootstrap too:
//Bootstrap.php
protected function _initUserSession()
{
return new Zend_Session_Namespace('user');
}
然后我将使用一个动作助手:
Then I would use an action helper:
// library/App/Controller/Action/Helper/Session.php
class App_Controller_Action_Helper_Session extends Zend_Controller_Action_Helper_Abstract
{
function direct()
{
return $this->getFrontController()->getParam('userSession');
}
}
p>
You access it in your controller like this:
function indexAction()
{
$session = $this->_helper->session;
}
这篇关于在Zend Framework中处理会话处理的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!