问题
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