我試圖訪問會話拋出不同的控制器或在一個控制器中的不同功能,但當我嘗試從不同的功能訪問值,變量爲NULL 我讀了一些文件(包括cakephp session cookbook)和更多,但我不明白如何配置會話之前使用它。cakephp 3會話變量在其他函數中空
我有一個控制器的問題,即時通訊嘗試使用會話,但價值爲零後即時通訊寫入另一個功能!?我的另一個問題是我如何避免寫$ this-> request-> 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';
}
}
感謝Adnvanced
您和Chuug解決方案結束於:錯誤:調用成員函數write()null 文件D:\ bookmark \ src \ Controller \ ComlibsController.php 行:20 –