Public
Authored by animate.rene.elsaesser

[TYPO3][Extbase] SessionHandler (Clipboard)

Controller:

    /**
     * Backend-Session
     *
     * @var \VENDOR\MyExt\Domain\Session\BackendSessionHandler
     * @inject
     */
    protected $backendSession;
    /**
     * Frontend-Session
     *
     * @var \VENDOR\MyExt\Domain\Session\FrontendSessionHandler
     * @inject
     */
    protected $frontendSession;
    /**
     * Frontend- or Backend-Session
     * The type of session is set automatically in initializeAction().
     *
     * @var \VENDOR\MyExt\Domain\Session\SessionHandler
     * @inject
     */
    protected $session;
    public function initializeAction() {

        // Backend-Session
        // Optional - setting the Session-Storage-Key on every action to keep it unique
        $this->backendSession->setStorageKey( $this->request->getControllerExtensionName() . $this->request->getPluginName() );

        // Frontend-Session
        // Optional - setting the Session-Storage-Key on every action to keep it unique
        $this->frontendSession->setStorageKey( $this->request->getControllerExtensionName() . $this->request->getPluginName() );

        // Automatic BE/FE-Session
        $this->session = new \VENDOR\MyExt\Domain\Session\SessionHandler(TYPO3_MODE);
        // Optional - setting the Session-Storage-Key on every action to keep it unique
        $this->session->setStorageKey( $this->request->getControllerExtensionName() . $this->request->getPluginName() );
    }

Examples for backend-session:

    public function myBackendAction() {
        $mySessionValue = "Some data to store in session";
        $this->backendSession->store( 'mySessionDataKey', $mySessionValue);

        // get a value from session
        $mySessionValue = $this->backendSession->get( 'mySessionDataKey');

        // remove a value from session
        $this->backendSession->delete( 'mySessionDataKey');
    }

Examples for frontend-session:

    public function myFrontendAction() {
        $mySessionValue = "Some data to store in session";
        $this->frontendSession->store( 'mySessionDataKey', $mySessionValue);

        // get a value from session
        $mySessionValue = $this->frontendSession->get( 'mySessionDataKey');

        // remove a value from session
        $this->frontendSession->delete( 'mySessionDataKey');
    }

Examples for an action which is called in backend and frontend:

    public function myActionUsedInBackendAndFrontendAction() {
        $mySessionValue = "Some data to store in session";
        $this->session->store( 'mySessionDataKey', $mySessionValue);

        // get a value from session
        $mySessionValue = $this->session->get( 'mySessionDataKey');

        // remove a value from session
        $this->session->delete( 'mySessionDataKey');
    }
Edited
Classes/Domain/Session/SessionHandler.php 2.52 KB
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment