Symfony2之session与cookie用法小结
本文实例讲述了Symfony2之session与cookie用法。分享给大家供大家参考,具体如下:
session操作:
1.SetSession:
publicfunctiontestSetSession(){ $session=$this->getRequest()->getSession(); $session->set($sessionName,$sessionValue); }
2.GetSession:
publicfunctiontestGetSession(){ $session=$this->getRequest()->getSession(); $username=$session->get($sessionName); }
3.ClearSession:
publicfunctiontestClearSession(){ $session=$this->getRequest()->getSession();//清除session $session->clear(); }
cookie操作:
1.SetCookie
useSymfony\Component\HttpFoundation\Response; useSymfony\Component\HttpFoundation\Cookie; publicfunctiontestSetCookie($name,$value,$expire=0){ $response=newResponse(); $response->headers->setCookie(newCookie($name,$value,time()+$expire)); $response->send();//包括sendHeaders()、sendContent() }
2.GetCookie:
publicfunctiontestGetCookie(){ $request=$this->getRequest(); return$request->cookies->all(); }
3.ClearCookie:
publicfunctiontestClearCookie(){ $response=newResponse(); $response->headers->setCookie(newCookie($name,$value,-1)); $response->send(); }
4.twig模板调用cookie:
{{app.request.cookies.get('cookie_name')}}
希望本文所述对大家基于Symfony框架的PHP程序设计有所帮助。