简体   繁体   English

我如何在cakephp 2中编写会话变量?

[英]How can i write session variable in view in cakephp 2?

I read the cakephp documentation but there example is given how to write session vairable in controller like following. 我阅读了cakephp文档,但给出了如何在控制器中编写会话可变的示例,如下所示。

$this->Session->write('Name', 'Value');

In a situation, I need to write session variable in view. 在某种情况下,我需要查看会话变量。 How can i achieve that. 我怎样才能做到这一点。

The question is old but the answer may help someone. 这个问题很旧,但是答案可能会帮助别人。 To write SESSION in view use. 在视图中编写SESSION。

<?php SessionComponent::write('Name', 'Value'); ?>

And to read it use 并阅读使用

$this->Session->read('Name');

You cannot write to the Session in views, only read. 您不能在视图中写入会话,而只能读取。 All Session writing is done in the Controller. 所有会话写入均在Controller中完成。

But if you really need, and I highly do not recommend doing this, write to the Session in the view you can use AJAX to call a method that writes whatever you need into the Session. 但是,如果您确实需要,并且我强烈建议您不要这样做,请在视图中写入Session,您可以使用AJAX调用将所需内容写入Session的方法。

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

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