我正在尝试使用 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
}
我已经在调试会话中单步执行了代码,我可以看到 cookie 值正在sfWebResponse
变量中进行整理——但是,没有设置任何 cookie——这在preExecute()
函数中得到了证明——检索到的所有 cookie 的值都为 null。
为什么会这样?