Sessions

Da Extbase keine Unterstützung für Sessions mitbringt, habe ich es selbst programmiert.

SessionHandler

Neue Klasse anlegen in: Classes/Domain/Session/SessionHandler.php

<?php
/***************************************************************
 *  Copyright notice
 *
 *  (c) 2011 Erwin Knoll <typo3coding@rootsystem.de>, Rootsystem
 * 
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/


/**
 * session handling
 *
 * @package rsysproductbase
 * @license www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
 *
 */
class Tx_Rsysproductbase_Domain_Session_SessionHandler implements t3lib_Singleton {
 
    /**
     * Return stored session data
     * @return Object the stored object
     */
    public function restoreFromSession() {
        $sessionData = $GLOBALS['TSFE']->fe_user->getKey('ses', 'tx_rsysproductbase');
        return unserialize($sessionData);
    }
 
    /**
     * Write session data
     * @param    $object    any serializable object to store into the session
     * @return    Tx_Rsysproductbase_Domain_Session_SessionHandler this
     */
    public function writeToSession($object) {
        $sessionData = serialize($object);
        $GLOBALS['TSFE']->fe_user->setKey('ses', 'tx_rsysproductbase', $sessionData);
        $GLOBALS['TSFE']->fe_user->storeSessionData();
        return $this;
    }
 
    /**
     * Clean up session
     * @return    Tx_Rsysproductbase_Domain_Session_SessionHandler this
     */
    public function cleanUpSession() {
        $GLOBALS['TSFE']->fe_user->setKey('ses', 'tx_rsysproductbase', NULL);
        $GLOBALS['TSFE']->fe_user->storeSessionData();
        return $this;
    }
}
?>

Einbinden

Im Controller:

/**
 * The session handler
 *
 * @var Tx_Rsysproductbase_Domain_Session_SessionHandler
 */
protected $sessionHandler = NULL;

/**
 * __construct
 *
 * @return
 */
public function __construct() {
    parent::__construct();
    // get an instance of the session handler
    $this->sessionHandler = t3lib_div::makeInstance('Tx_Rsysproductbase_Domain_Session_SessionHandler');
}

oder:

/**
 * injectSessionHandler
 *
 * @param Tx_Rsysquiz_Domain_Session_SessionHandler $sessionHandler
 * @return void
 */
public function injectSessionHandler(Tx_Rsysquiz_Domain_Session_SessionHandler $sessionHandler) {
    $this->sessionHandler = $sessionHandler;
}

Verwenden

if(!empty($sort)) {
    $this->sessionHandler->writeToSession(array('sort'=>$sort));
} else {
    $sort = $this->sessionHandler->restoreFromSession('sort');
}

Erstellt: 11/2011| Geändert: 10/2015