Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii can't access session variables

Tags:

php

session

yii

My problem: I can't access session data in the view, which I tried to save in the controller before rendering the view. In my opinion there's an error when storing the session data. (It's necessary for me to modify the session data after creating it, not only in the single action.)

ProcessController.php

public function actionNew() {

    $formhash = md5(time());
    $hashList = array();
    $hashList[$formhash]['processingNumber'] = '';
    //fill with empty model
    $hashList[$formhash]['model'] = $this->loadModel();
    //store hashList in session
    Yii::app()->session['hashList'] = $hashList;

    $this->render('/process', array('hashValue => $formHash));

}

Now in the view I need the data from the session to show them to the user. But when dumping the hashList it just dumps "null" (Maybe because the saving in the controller didn't went well).

process.php

<?php
$form = this->beginWidget('CActiveForm', array(
    'id' => 'process_form',
    //several other things...
));
//Output: null
CVarDumper::dump(Yii::app()->session['hashList'],10,true);
?>

I tried to use $_SESSION instead of Yii::app()->session, which gives me access to the data in the view. But when handling other actions in the Controller the $_SESSION variable is undefined.

Any suggestions?

Thank you.

like image 971
filla2003 Avatar asked Nov 10 '22 08:11

filla2003


1 Answers

Long answer is:

Regarding to this documents:

$session=new CHttpSession;
$session->open();
$value1=$session['name1'];  // get session variable 'name1'
$value2=$session['name2'];  // get session variable 'name2'
foreach($session as $name=>$value) // traverse all session variables
$session['name3']=$value3;  // set session variable 'name3'

You can use as well:

Yii::app()->session->set('hashList', $hashList);
Yii::app()->session->get('hashList');

And set it again.

Beside this session thing, why do not you use this:

$this->render('/process', array('hashValue => $formHash, 'hashList' => $hashList));

So you do not need to save it in a session if you can reach it directly into the view.

like image 82
Jurik Avatar answered Nov 15 '22 09:11

Jurik