php CakePHP 会话变量在视图页面中使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1596953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:13:33  来源:igfitidea点击:

CakePHP Session variable use in view page?

phpsessioncakephp

提问by coderex

I have a variable contaning "username" and want to get these values via session to any of the view pages.

我有一个包含“用户名”的变量,并希望通过会话获取这些值到任何视图页面。

How can I get this session variable in the view ?

如何在视图中获取此会话变量?

回答by deceze

There's a SessionComponentavailable in your Controller that you can use like $this->Session->write('Name', 'Value');. Analogously there's also the SessionHelperfor the View, which does very similar things and can be used like $session->read('Name');.

您的控制器中有一个SessionComponent可用,您可以像$this->Session->write('Name', 'Value');. 类似地,还有用于 View的SessionHelper,它做的事情非常相似,可以像$session->read('Name');.

回答by trante

You can use $this->Session->read('myParam')in your Views files.
But you can't use $this->Session->write('myParam').

您可以$this->Session->read('myParam')在您的视图文件中使用。
但是你不能使用$this->Session->write('myParam').

As noted here:

正如指出的在这里

The major difference between the Session Helper and the Session Component is that the helper does not have the ability to write to the session.

Session Helper 和 Session Component 的主要区别在于 Helper 没有写入会话的能力。

回答by Andy

To use it in a helper you must remember to include it in your $helpers declaration:

要在助手中使用它,您必须记住将它包含在您的 $helpers 声明中:

class Foo extends AppHelper
{
    var $helpers = array('Session','Bar');

回答by Travis Leleu

If you're in the controller, use the Session component. It's included, by default, in all the controllers. It has the Session::read() and Session::write() methods. Check out http://book.cakephp.org/view/173/Sessionsfor more information.

如果您在控制器中,请使用 Session 组件。默认情况下,它包含在所有控制器中。它有 Session::read() 和 Session::write() 方法。查看http://book.cakephp.org/view/173/Sessions了解更多信息。

I believe, if the Session component is like some of the other components, you can use it inside the views. Try just doing $session->read() in your view code blocks. If that doesn't work, try doing a $this->Session->read(...). As a last resort, if none of those work, you can always use the good old PHP $_SESSION, although it's kinda veering outside the Cake framework. However, if you are sure you're not going to use Cake's Session management (and you don't really have to, IMO, as it's little more than a wrapper around $_SESSION), then just know when to properly apply the hack.

我相信,如果 Session 组件像其他一些组件一样,您可以在视图中使用它。尝试在您的视图代码块中执行 $session->read() 。如果这不起作用,请尝试执行 $this->Session->read(...)。作为最后的手段,如果这些都不起作用,您总是可以使用很好的旧 PHP $_SESSION,尽管它有点偏离 Cake 框架。但是,如果您确定您不会使用 Cake 的会话管理(并且您真的不必使用,IMO,因为它只不过是 $_SESSION 的包装器),那么只需知道何时正确应用 hack。

回答by Hyman

User this in you App controller's before filter

在您的应用程序控制器的过滤器之前使用它

$this->Session->write('Person.eyeColor', 'username'); $green = $this->Session->read('Person.eyeColor'); $this->set('username',$green);

$this->Session->write('Person.eyeColor', '用户名'); $green = $this->Session->read('Person.eyeColor'); $this->set('用户名',$green);

This will give you result as you want

这会给你你想要的结果

回答by Kiran

You can pass the Session object from controller to a view template using

您可以使用 Session 对象从控制器传递到视图模板

$this->set('session',$this->Session);

Then in view file use $session->read('SessionName');

然后在视图文件中使用 $session->read('SessionName');