2016-05-11 10 views
0

セッションにアクセスしようとしていますが、別のコントローラを別のコントローラに置いていますが、別の関数の値にアクセスしようとすると変数がNULLになります。 いくつかのドキュメント(cakephp session cookbookを含む)より多くの、しかし、私はそれを使用する前にセッションを構成する方法について少し理解できませんでした。cakephp 3セッション変数が他の関数で空になっています

セッションを使用しようとしている1つのコントローラに問題がありますが、別の関数で値を書き込んだ後に値がnullになります。

私の他の質問は、$ this-> request-> session()を書くのを避ける方法です。私はセッションを使用するすべての機能で。 CODE:あなたはwritereadセッションデータのためのこれらのコードを使用しAdnvanced

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

おかげ

答えて

0

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

CakePHPの本の中で、このsectionをお読みください。

+0

両方にあなたを試してみて、 Chuugソリューションは次のようになります。エラー:メンバー関数write()on null null ファイルD:¥bookmark¥src¥Controller¥ComlibsController.php 行:20 –

0

代わり

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

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

を試してみて、

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

代わりの

$avoidedIDs = $session->read('avoidedID'); 
関連する問題