我正在尝试使用Symfony 1.4框架将变量存储在cookie中。下面是我的sfAction派生类的一个片段:
class productActions extends sfActions
{
public function preExecute()
{
$this->no_registration_form = true;
$request = $this->getRequest();
$cookie_value = $request->getCookie('pcatid');
$this->prod_category_id = (!isset($cookie_value)) ? 0 : $cookie_value;
$cookie_value = $request->getCookie('pitid');
$this->product_item_id = (!isset($cookie_value)) ? 0 : $cookie_value;
$cookie_value = $request->getCookie('pcatnm');
$this->product_category_name = (!isset($cookie_value)) ? null : base64_decode ($cookie_value);
$cookie_value = $request->getCookie('pipnm');
$this->product_info_partial_name = (!isset($cookie_value)) ? null : $cookie_value;
$cookie_value = $request->getCookie('protl');
$this->title = (!isset($cookie_value)) ? null : base64_decode ($cookie_value);
}
public function postExecute()
{
$expire = time()+2592000;
$path = '/products/';
$response = $this->getResponse();
$value = $this->prod_category_id;
if (isset($value))
$response->setCookie('pcatid', $value, $expire, $path);
$value = $this->product_item_id;
if (isset($value))
$response->setCookie('pitid', $value, $expire, $path);
$value = base64_encode($this->product_category_name);
if (isset($value))
$response->setCookie('pcatnm', $value, $expire, $path);
$value = $this->product_info_partial_name;
if (isset($value))
$response->setCookie('pipnm', $value, $expire, $path);
$value = base64_encode($this->title);
if (isset($value))
$response->setCookie('protl', $value, $expire, $path);
}
public function executeIndex(sfWebRequest $request)
{
// uses defaults or settings stored in cookies
}
... // other functions that set the required instance member fields
}
我已经在调试会话中逐步完成了代码,我可以看到在sfWebResponse
变量中对cookie值进行了整理-但是,没有设置cookie-这在preExecute()
函数中得到了演示-检索到的所有cookie的值都为null。
为什么会发生这种情况?
我对一个新的symfony项目进行了一个简单的测试。
我已经复制/粘贴了你的preExecute
&postExecute
和我添加了一个带有空模板的简单操作:
public function executeIndex(sfWebRequest $request)
{
var_dump($_COOKIE);
$this->prod_category_id = 123;
$this->product_item_id = 456;
$this->product_category_name = 789;
$this->product_info_partial_name = 159;
$this->title = 753;
}
我把这行$path = '/products/';
改成了$path = '/';
。
第一次尝试时,我没有饼干,所以我什么都没有:
array
empty
在刷新时,cookie被定义了,我可以看到它们:
array
'symfony' => string 'c04khrspp5fmg3sh797uq9c9s1' (length=26)
'pcatid' => string '123' (length=3)
'pitid' => string '456' (length=3)
'pcatnm' => string 'Nzg5' (length=4)
'pipnm' => string '159' (length=3)
'protl' => string 'NzUz' (length=4)
您的代码中可能出现的错误是您将cookie路径设置为/products/
。这意味着,当您在http://exemple.com/products/...
上时,您只能访问这些cookie。
如果您尝试在http://exemple.com/
上访问这些cookie,您将一无所获。
如果这不能解决你的问题,你可以用定义的uri(至少是域后面的部分)粘贴一个操作的例子吗。
当您将$path传递给方法时,您只能在从同一URL访问cookie时使用cookie。
如果你想从所有网站访问cookie,请不要在路径中传递任何内容。
$response = $this->getResponse();
$response->setCookie('stack', 'This cookie is readable from all site for the next 60 segs.', time() + 60);
$response->setCookie('overflow', 'This cookie is readable from mypage for the next 60 segs.', time() + 60, 'http://mysite.com/mypage');