Cakephp 2.0无法获取或读取会话视图文件?

时间:2016-08-02 08:46:43

标签: session cakephp view controller cakephp-2.x

我正在查看ctp文件中显示或读取会话,但是控制器显示是会话创建的,并且读取会话也显示在控制器中但不能在视图ctp文件中显示或读取会话?< / p>

控制器功能

var $components = array('Auth','Session','RequestHandler','Email');
$selectedlocation= $_POST['location'];
$this->Session->write('homepagelocation.selectlocation', $selectedlocation);
echo $this->Session->read('homepagelocation.selectlocation');

ctp文件中的会话阅读方法

echo $this->Session->read('homepagelocation.selectlocation');

3 个答案:

答案 0 :(得分:2)

要从视图中的控制器访问数据,您需要将数据设置为视图。

var $components = array('Auth','Session','RequestHandler','Email');
$selectedlocation= $_POST['location'];
$this->Session->write('homepagelocation.selectlocation', $selectedlocation);
$this->set('location', $this->Session->read('homepagelocation.selectlocation'));

但是,我会问,为什么你在会话中写入数据,从中读取数据并在你已经访问$selectedlocation所需的数据时将其设置为视图?

答案 1 :(得分:0)

埃洛,伙计。我认为$ _POST []不会以这种方式进入你的控制器,你应该尝试:

$this->request->data['location']; //Cake 2.x
$this->data['location']; //Cake 1.3

然后将会话设置为视图:

$this->set('location', $this->Session->read('homepagelocation.selectlocation')); 

现在您可以在视图上打印:

echo $location;

答案 2 :(得分:0)

您可以在此处查看:http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html 可以在视图中使用Session read方法,请在Controller中将Session设置为$ helper元素。