[英]Best way to deal with session handling in Zend Framework
因此,我開始使用Zend框架,並希望實現站點范圍的“用戶”會話。...我可以輕松地從應用程序中的所有模塊/控制器進行訪問。
我想在庫中創建一個新的名稱空間並擴展控制器,例如:
class MYCUSTOMLIB_Controller_Action extends Zend_Controller_Action
{
protected $_userSession;
function preDispatch(Zend_Controller_Request_Abstract $req)
{
$this->_userSession = new Zend_Session_Namespace('user');
}
}
然后讓我所有的控制器/模塊/等都從中擴展嗎?
還是應該創建一個插件或什么? 您將如何制作此插件以將用戶會話傳遞給控制器?
還是在引導程序中執行? 再次如何傳遞給控制器?
另外,我應該使用Zend_Session_Namespace還是Zend_Http_Cookie,還應該如何加密和XSS清除cookie或自動完成?
我也會在引導程序中初始化:
//Bootstrap.php
protected function _initUserSession()
{
return new Zend_Session_Namespace('user');
}
然后,我將使用一個動作助手:
// 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');
}
}
您可以像這樣在控制器中訪問它:
function indexAction()
{
$session = $this->_helper->session;
}
您應該在引導程序中初始化會話。 您可以將其放在Zend_Registry中並以這種方式進行訪問,也可以從控制器中調用$ this-> getInvokeArg('bootstrap')來訪問引導程序。
// in your controllers
public function init()
{
$bootstrap = $this->getInvokeArg('bootstrap');
$this->_session = $bootstrap->getResource('session');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.