本文实例讲述了Symfony2之session与cookie用法。分享给大家供大家参考,具体如下:
session操作:
1. Set Session:
getRequest()->getSession();
$session->set($sessionName,$sessionValue );
}
2. Get Session:
getRequest()->getSession();
$username = $session->get($sessionName);
}
3. Clear Session:
getRequest()->getSession();//清除session
$session->clear();
}
cookie操作:
1. Set Cookie
headers->setCookie(new Cookie($name,time() + $expire));
$response->send(); // 包括 sendHeaders()、sendContent()
}
2. Get Cookie:
getRequest();
return $request->cookies->all();
}
3. Clear Cookie:
headers->setCookie(new Cookie($name,-1));
$response->send();
}
4. twig模板调用cookie:
PHP;">
{{ app.request.cookies.get('cookie_name') }}
希望本文所述对大家基于Symfony框架的PHP程序设计有所帮助。