Session 键入3 Extbase设置并从会话获取值
我正在typo3v6.1上编写一个extbase扩展 那个分机可以预订公共汽车票。 在这里我的计划是什么,用户将选择日期和座位数,并提交表格 在这里,我的计划是将所选席位的日期和费率推送到会议(篮子)。 在付款时,我想从会话中获取该值,付款后我需要清除特定会话 简而言之,如何在extbase中向会话推送和从会话检索值。 有什么建议吗?Session 键入3 Extbase设置并从会话获取值,session,typo3,extbase,Session,Typo3,Extbase,我正在typo3v6.1上编写一个extbase扩展 那个分机可以预订公共汽车票。 在这里我的计划是什么,用户将选择日期和座位数,并提交表格 在这里,我的计划是将所选席位的日期和费率推送到会议(篮子)。 在付款时,我想从会话中获取该值,付款后我需要清除特定会话 简而言之,如何在extbase中向会话推送和从会话检索值。 有什么建议吗? 谢谢。有不同的方法。最简单的方法是在会议中写作 $GLOBALS['TSFE']->fe_user->setKey("ses","key",$valu
谢谢。有不同的方法。最简单的方法是在会议中写作
$GLOBALS['TSFE']->fe_user->setKey("ses","key",$value)
$GLOBALS["TSFE"]->fe_user->getKey("ses","key")
以及从会话中读取值
$GLOBALS['TSFE']->fe_user->setKey("ses","key",$value)
$GLOBALS["TSFE"]->fe_user->getKey("ses","key")
我用的是服务类
<?php
class Tx_EXTNAME_Service_SessionHandler implements t3lib_Singleton {
private $prefixKey = 'tx_extname_';
/**
* Returns the object stored in the user´s PHP session
* @return Object the stored object
*/
public function restoreFromSession($key) {
$sessionData = $GLOBALS['TSFE']->fe_user->getKey('ses', $this->prefixKey . $key);
return unserialize($sessionData);
}
/**
* Writes an object into the PHP session
* @param $object any serializable object to store into the session
* @return Tx_EXTNAME_Service_SessionHandler this
*/
public function writeToSession($object, $key) {
$sessionData = serialize($object);
$GLOBALS['TSFE']->fe_user->setKey('ses', $this->prefixKey . $key, $sessionData);
$GLOBALS['TSFE']->fe_user->storeSessionData();
return $this;
}
/**
* Cleans up the session: removes the stored object from the PHP session
* @return Tx_EXTNAME_Service_SessionHandler this
*/
public function cleanUpSession($key) {
$GLOBALS['TSFE']->fe_user->setKey('ses', $this->prefixKey . $key, NULL);
$GLOBALS['TSFE']->fe_user->storeSessionData();
return $this;
}
public function setPrefixKey($prefixKey) {
$this->prefixKey = $prefixKey;
}
}
?>
现在您可以像这样使用这个会话处理程序
// Write your object into session
$this->sessionHandler->writeToSession('KEY_FOR_THIS_PROCESS');
// Get your object from session
$this->sessionHandler->restoreFromSession('KEY_FOR_THIS_PROCESS');
// And after all maybe you will clean the session (delete)
$this->sessionHandler->cleanUpSession('KEY_FOR_THIS_PROCESS');
使用扩展名重命名Tx_EXTNAME和Tx_EXTNAME,并注意将会话处理程序类放入正确的目录(Classes->Service->SessionHandler.php)
您可以存储任何数据,而不仅仅是对象
HTH从Typo3 v7中,您还可以复制表单的本机会话处理程序(),并根据需要进行更改。该类使普通用户和登录用户之间的会话不同,它支持由sessionPrefix分隔的多个会话数据 我也做了同样的事情,为了一个更为普遍的目的,对这个类进行了概括。我只删除了一个方法,更改了变量名称并添加了方法hasSessionKey()。以下是我的完整示例:
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
/**
* Class SessionUtility
*
* this is just a adapted version from \TYPO3\CMS\Form\Utility\SessionUtility,
* but more generalized without special behavior for form
*
*
*/
class SessionUtility {
/**
* Session data
*
* @var array
*/
protected $sessionData = array();
/**
* Prefix for the session
*
* @var string
*/
protected $sessionPrefix = '';
/**
* @var TypoScriptFrontendController
*/
protected $frontendController;
/**
* Constructor
*/
public function __construct()
{
$this->frontendController = $GLOBALS['TSFE'];
}
/**
* Init Session
*
* @param string $sessionPrefix
* @return void
*/
public function initSession($sessionPrefix = '')
{
$this->setSessionPrefix($sessionPrefix);
if ($this->frontendController->loginUser) {
$this->sessionData = $this->frontendController->fe_user->getKey('user', $this->sessionPrefix);
} else {
$this->sessionData = $this->frontendController->fe_user->getKey('ses', $this->sessionPrefix);
}
}
/**
* Stores current session
*
* @return void
*/
public function storeSession()
{
if ($this->frontendController->loginUser) {
$this->frontendController->fe_user->setKey('user', $this->sessionPrefix, $this->getSessionData());
} else {
$this->frontendController->fe_user->setKey('ses', $this->sessionPrefix, $this->getSessionData());
}
$this->frontendController->storeSessionData();
}
/**
* Destroy the session data for the form
*
* @return void
*/
public function destroySession()
{
if ($this->frontendController->loginUser) {
$this->frontendController->fe_user->setKey('user', $this->sessionPrefix, null);
} else {
$this->frontendController->fe_user->setKey('ses', $this->sessionPrefix, null);
}
$this->frontendController->storeSessionData();
}
/**
* Set the session Data by $key
*
* @param string $key
* @param string $value
* @return void
*/
public function setSessionData($key, $value)
{
$this->sessionData[$key] = $value;
$this->storeSession();
}
/**
* Retrieve a member of the $sessionData variable
*
* If no $key is passed, returns the entire $sessionData array
*
* @param string $key Parameter to search for
* @param mixed $default Default value to use if key not found
* @return mixed Returns NULL if key does not exist
*/
public function getSessionData($key = null, $default = null)
{
if ($key === null) {
return $this->sessionData;
}
return isset($this->sessionData[$key]) ? $this->sessionData[$key] : $default;
}
/**
* Set the s prefix
*
* @param string $sessionPrefix
*
*/
public function setSessionPrefix($sessionPrefix)
{
$this->sessionPrefix = $sessionPrefix;
}
/**
* @param string $key
*
* @return bool
*/
public function hasSessionKey($key) {
return isset($this->sessionData[$key]);
}
}
别忘了先调用initSession,每次你想使用这个类的任何方法时有没有用于shop的extbase扩展的例子?在上面的情况下,它将是“ses”而不是“user”。要获得已经设置的值…谢谢。别忘了
$GLOBALS['TSFE']->fe_user->storeSessionData()代码>在设置键之后这是最好的方法。不需要额外的序列化/取消序列化,TYPO3会话将自动处理。