Search code examples
zend-frameworkyii2

Need Yii2 Equivalent of Zend_Session_Namespace


I am currently migrating an old Zend 1.1 website and need a replacement for the uses of Zend_Session_Namespace.

Does one exist for Yii2? Or alternatively is there a plugin or something to add this functionality?

-Edit: Specifically the ability to set expiry timeouts and hop limits like Zend has.

Thank you.


Solution

  • UPDATE

    The info you have added in the edit was never mentioned earlier and makes your question too broad you might create a separate question for that.

    By default session data are stored in files. The implementation is locking a file from opening a session to the point it's closed either by session_write_close() (in Yii it could be done as Yii::$app->session->close()) or at the end of request. While session file is locked all other requests which are trying to use the same session are blocked i.e. waiting for the initial request to release the session file. this can work for dev or small projects. But when it comes to handling massive concurrent requests, it is better to use more sophisticated storage, such as a database.


    Zend_Session_Namespace instances provide the primary API for manipulating session data in the Zend Framework. Namespaces are used to segregate all session data, if you are converting the script to Yii2 framework you might need to look into https://www.yiiframework.com/doc/api/2.0/yii-web-session

    A simple example to compare both of the functionalities by example are

    Zend Framework 1.1 Counting Page Views

    $defaultNamespace = new Zend_Session_Namespace('Default');
    
    if (isset($defaultNamespace->numberOfPageRequests)) {
        // this will increment for each page load.
        $defaultNamespace->numberOfPageRequests++;
    } else {
        $defaultNamespace->numberOfPageRequests = 1; // first time
    }
    
    echo "Page requests this session: ",
        $defaultNamespace->numberOfPageRequests;
    

    Yii2 Framework Counting Page Views

    public function actionIndex()
    {
        $session = new \yii\web\Session();
        $session->open();
        $visits =   $session->get('visits', 0);
        $visits = $visits+1;
        $session->set('visits', $visits);
        return "Total visits $visits"; 
    }