Gan*_*ech 3 session typo3 extbase
我在typo3 v6.1上写了一个extbase扩展名.扩展程序假设要做一张公交车票预订.在我的计划中,用户将选择日期和座位数并提交表格.
在这里,我计划将所选座位的日期和费率推送到会话(篮子).在付款时,我希望从会话中获得价值,付款后我需要清除该特定会话.
简而言之,如何在extbase中推送和检索会话中的值.有什么建议 ?谢谢.
有不同的方式.最简单的是在会话中写作
$GLOBALS['TSFE']->fe_user->setKey("ses","key",$value)
Run Code Online (Sandbox Code Playgroud)
并从会话中读取值
$GLOBALS["TSFE"]->fe_user->getKey("ses","key")
Run Code Online (Sandbox Code Playgroud)
小智 5
我正在使用这个服务类.
<?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;
}
}
?>
Run Code Online (Sandbox Code Playgroud)
将此类注入控制器
/**
*
* @var Tx_EXTNAME_Service_SessionHandler
*/
protected $sessionHandler;
/**
*
* @param Tx_EXTNAME_Service_SessionHandler $sessionHandler
*/
public function injectSessionHandler(Tx_EXTNAME_Service_SessionHandler $sessionHandler) {
$this->sessionHandler = $sessionHandler;
}
Run Code Online (Sandbox Code Playgroud)
现在您可以像这样使用此会话处理程序.
// 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');
Run Code Online (Sandbox Code Playgroud)
使用您的扩展名重命名Tx_EXTNAME和tx_extname,并注意将会话处理程序类放入正确的目录(Classes - > Service - > SessionHandler.php).
您可以存储任何数据,而不仅仅是对象.
HTH
从Typo3 v7,您还可以复制表单的本机会话处理程序(\ TYPO3 \ CMS \ Form \ Utility \ SessionUtility)并将其更改为您的需要。该类在普通用户和登录用户之间有所不同,它支持由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]);
}
}
Run Code Online (Sandbox Code Playgroud)
每次要使用此类的任何方法时,请不要忘记先调用initSession