cakephp: Cookie does not read at view

时光总嘲笑我的痴心妄想 提交于 2019-12-11 10:46:21

问题


in controller i try this, and print $cookieee,its give me array

$this->Cookie->write('User', $cookie, true, '+2 weeks');

$cookieee = $this->Cookie->read('User')
echo "<pre>";
print_r($cookieee);
echo "</pre>";

but My question

when i print it any view file ,it doesnt print any values.

i try to print it by using below

echo "<pre>";
print_r($cookie);
echo "</pre>";
echo $cookie['username']."=cokie=";

and

$cookieee = $this->Cookie->read('User');
echo "<pre>";
print_r($cookieee);
echo "</pre>";

回答1:


your view does not have access to the cookie.. Basically $this is referring to two different things in the case of your controller and your view.. thus basically set it in a model that is passed to the view i.e viewmodel and then use that in your view.

Not famililar with PHP hence no code but this is my understanding based on using MVC framework in .NEt

I guess this question tells you how to do the same in PHP: how to read cookie value in cakephp view file

and if you want to break the MVC pattern here you could use: $_COOKIE[<cookie_name>].




回答2:


There is no Cookie helper in CakePHP, neither a method to access it in View.

You might want to set the cookie to a variable in controller, and then access that variable in corresponding view.



来源:https://stackoverflow.com/questions/7525428/cakephp-cookie-does-not-read-at-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!