简体   繁体   English

cakephp 3会话变量在其他函数中为空

[英]cakephp 3 session variable empty in other functions

Im trying to access session throw different controllers OR in different functions in one controller but when i try access the value from different function , variable is NULL i read some documents (including cakephp session cookbook ) and more , but i couldnt understand a bit on how to configure the session before using it. 我试图访问会话时会抛出不同的控制器,或者在一个控制器中使用不同的函数,但是当我尝试从不同的函数访问值时,变量为NULL,我读了一些文档(包括cakephp会话菜谱 )等等,但是我对此一无所知在使用前配置会话。

i have a problem in one controller that im trying to use a session but the value is null after im writing it in another function!? 我在一个控制器中遇到问题,我正在尝试使用会话,但在另一个函数中将其写入后,该值为null!

my other question is how can i avoid writing $this->request->session(); 我的另一个问题是如何避免编写$ this-> request-> session(); in every function that i want to use session. 在我要使用会话的每个功能中。 CODE : 代码:

<?php
namespace App\Controller;

use App\Controller\AppController;
use App\Model\Entity\Answer;
use Cake\ORM\TableRegistry;


class ComlibsController extends AppController {

    public function index() {

    }

    public function getResult(){

    $this->viewBuilder()->layout('comlib'); //change the layout from default
        $live_req = $this->request->data['searchBox']; // get the question

        $session->write('comlib/question' , $live_req);
            $query = $this->Comlibs->LFA($live_req); // get the first answer
            $shown_answerID = array($query[0]['answers'][0]['id'], '2');
    $this->Session->write(['avoidedID'  => $shown_answerID]); //avoid the answer that user saw to repeat agian

              $this->set('question',$query[0]['question']); // does work

        //get the answers result 
        //and send the array to the 
                            $fields = array('id','answer','rate' , 'view' , 'helpful'); 
                            for ($i = 0 ; $i  <= 6 ; $i++) {

                                $this->set('id'.$i,$query[0]['answers'][$i][$fields[0]]);
                                $this->set('answer'.$i,$query[0]['answers'][$i][$fields[1]]);
                                $this->set('rate'.$i,$query[0]['answers'][$i][$fields[2]]);
                                $this->set('view'.$i,$query[0]['answers'][$i][$fields[3]]);
                                $this->set('helpful'.$i,$query[0]['answers'][$i][$fields[4]]);


                            }       

    }
    public function moveon() {

    $this->render('getResult');
        $this->viewBuilder()->layout('comlib');
    echo    $question = $session->read('comlib/question'); // empty value echo

    $avoidedIDs = $session->read('avoidedID'); 
    var_dump($avoidedIDs); // returns NULL WHY ?
        $theid = $this->request->here;
        $query = $this->Comlibs->LFM($theid,$avoidedIDs,$question);

            echo 'imcoming';
    }
}

Thanks In Adnvanced 感谢进阶

You use these codes for write and read session data: 您可以使用以下代码来writeread会话数据:

$this->request->session()->write($name, $value); $this->request->session()->read($name);

Please read this section in CakePHP book. 请阅读CakePHP书中的本

Try 尝试

$this->session()->write('avoidedID', $shown_answerID);

instead of 代替

$this->session()->write(['avoidedID'  => $shown_answerID]);

And try 试一下

$avoidedIDs = $this->session()->read('avoidedID'); 

instead of 代替

$avoidedIDs = $session->read('avoidedID');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM