【问题标题】:cakephp 3 session variable empty in other functionscakephp 3会话变量在其他函数中为空
【发布时间】:2016-05-11 13:03:49
【问题描述】:

我试图访问会话在一个控制器中抛出不同的控制器或不同的函数,但是当我尝试从不同的函数访问值时,变量为 NULL 我阅读了一些文档(包括cakephp session cookbook)等等,但在使用之前我无法理解如何配置会话。

我在一个控制器中遇到问题,我尝试使用会话,但在我将其写入另一个函数后,该值为 null!?

我的另一个问题是如何避免编写 $this->request->session();在我想使用会话的每个功能中。 代码:

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

感谢高级

【问题讨论】:

    标签: php session cakephp cakephp-3.0


    【解决方案1】:

    您将这些代码用于writeread 会话数据:

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

    请阅读 CakePHP 书籍中的 section

    【讨论】:

    • 你和 Chuug 解决方案都以 :Error: Call to a member function write() on null File D:\bookmark\src\Controller\ComlibsController.php Line: 20
    【解决方案2】:

    试试

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

    而不是

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

    然后试试

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

    而不是

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-12
      • 2021-12-30
      • 2020-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-03
      相关资源
      最近更新 更多