如何在CakePHP中访问.js文件中的会话变量 [英] How to access a session variable in a .js file in CakePHP
问题描述
在我的 CakePHP 应用程序中,我正在某个控制器中创建会话变量,我需要访问 .js文件。
In my CakePHP application I am creating a session variable in some controller, which I need to access in a ".js" file. Is there any possible way to do this?
推荐答案
我建议将它们作为这样的参数传递-
I would suggest passing them as parameters like this -
<a onclick="some_function('<?php echo $_SESSION['var'] ?>')">Click</a>
并具有javascript函数 some_function()
处理它,而不是内联分配它们。这样,您就可以从服务器端代码中释放javascript文件。这是最干净的方法。
and have the javascript function some_function()
process it instead of assigning them inline. This way you can have the javascript file free from server side code. This is the cleanest way.
我也觉得由于会话变量是服务器端的,因此不应通过内联代码直接用客户端语言处理它们。
I too feel that because session variables are server side, they should not be handled directly in client side language through inline code.
这篇关于如何在CakePHP中访问.js文件中的会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!