如何将Zend_http_cookie值转换为另一个函数或页面

时间:2012-02-24 10:48:32

标签: zend-framework

我有以下函数searchAction()和recentsearchAction()。我将我的cookie值存储为$ row,这是一个数组变量。问题是我想在recentsearchAction()方法中获取我的cookie值。怎么弄它。请帮助。我是Zend的新人。

public function searchAction()     {

    $form=new Application_Form_Search();               

    $this->view->form = $form;        

    if($this->getRequest()->isPost()){

        $formData = $this->getRequest()->getPost();              

        if($form->isValid($formData)){

            $uname=$form->getValue('uname');                

            $search = new Application_Model_DbTable_Hobbies();

            $row=$search->searchHobby($uname);         

            $cookie = new Zend_Http_Cookie('cookiename',$row,'localhost',time() + 7200);           

            echo $cookie->getName();

            echo $cookie->getValue();

            echo $cookie->getDomain();

            echo $cookie->getPath();

            echo $cookie->getExpiryTime();                                

            $this->view->search=$row;                                                                                                 

        }
    }
}    

public function recentsearchAction()
{                

}    

2 个答案:

答案 0 :(得分:0)

我知道在ZF中保存数据的最简单方法是将数据转储到Zend_Session_Namespace,这将保留数据,直到您取消设置或覆盖它为止。 Zend_session将接受字符串,对象和数组。

基本用法:

//save data to namespace named cookie
$sesion = new Zend_Session_Namespace('cookie');
//namespace = cookie, data = cookieData
$session->cookieData = $cookieData;

//get data from namespace in another controller or action
//every time you need to access the namespace you have instantiate Zend_Session_Namespace
//I usually put a line in init() of each controller assigning it to a protected property
protected $_sesion = new Zend_Session_Namespace('cookie');
//access array or objects parts in session in normal ways
//assuming session is protected/private property
echo $this->_session->cookieData->username;//if object
echo $this->_session->cookieData['email'];//if array, I think
echo $this->_sesion->cookieData; //if single string

在Zend_Session_Namespace中取消设置数据

Zend_Session::namespaceGet('cookie');
Zend_Session::namespaceUnset('cookie');

我通常会将要存储的数组转换为对象$data = (object)array('data'),以使访问更简单。

答案 1 :(得分:0)

试试这个:

$session=Zend_Session_Namespace("cookie");
$session->setExpirationSeconds(60);
$session->username;
$session->email;

现在,您可以在任何页面中使用此会话命名空间,如:

$session=Zend_Session_Namespace("cookie");
echo "User Name : ".$session->username;
echo "Password : ".$session->email;

它将保留您的数据60秒。您可以提供尽可能多的到期时间。