Typo3 Extbase Set and Get values from Session

后端 未结 3 429
有刺的猬
有刺的猬 2020-12-19 16:36

I am writing an extbase extension on typo3 v6.1 That extension suppose to do a bus ticket booking. Here what my plan is, user will select date and number of seats and submit

3条回答
  •  春和景丽
    2020-12-19 17:06

    I'm using for this a service class.

    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;
        }
    
    }
    ?>
    

    Inject this class into your controller

    /**
     *
     * @var Tx_EXTNAME_Service_SessionHandler
     */
    protected $sessionHandler;
    
    /**
     * 
     * @param Tx_EXTNAME_Service_SessionHandler $sessionHandler
     */
    public function injectSessionHandler(Tx_EXTNAME_Service_SessionHandler $sessionHandler) {
        $this->sessionHandler = $sessionHandler;
    }
    

    Now you can use this session handler like this.

    // 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');
    

    Rename Tx_EXTNAME and tx_extname with your extension name and pay attention to put the session handler class into the right directory (Classes -> Service -> SessionHandler.php).

    You can store any data, not only objects.

    HTH

提交回复
热议问题