symfony2 symofny3中得到get post session cookies的方法
Posted 飞羽
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了symfony2 symofny3中得到get post session cookies的方法相关的知识,希望对你有一定的参考价值。
symfony升级以后,废弃了以前的post,get等方法
之前是这样写的,直接上代码咯
public function webTestAction(Request $request) { //symfony2的get $this->getRequest()->get(); //symfony3的get $test = $request->query->get(‘test‘); //symfony2的post $this->getRequest()->post(); //symfony3的post $test = $request->request->get(‘test‘); return $this->render(‘test.html.twig‘, array( )); }
session的操作,
1.symfony2的session日常操作
1.set session 生成session
public function testSetSession() { $session = $this->getRequest()->getSession(); $sessionValue = array( array(‘username‘ => ‘gaofei‘,‘password‘ => 12), ); $session->set(‘userinfo‘, $sessionValue ); }
2.Get Session 拿取session
public function testGetSession() { $session = $this->getRequest()->getSession(); $sessionName = ‘userinfo‘; $username = $session->get($sessionName); }
3.Clear Session清除session
public function testClearSession() { $session = $this->getRequest()->getSession();//清除session $session->clear(); }
2.symfony3的session操作
set session
public function sessionAction(Request $request) { $session = $request->getSession(); $sessionValue = array( array(‘username‘ => ‘gaofei‘,‘password‘ => 12), ); $session->set(‘userinfo‘, $sessionValue ); }
get session
public function sessionAction(Request $request) { $session = $request->getSession(); $username = $session->get(‘userinfo‘); var_dump($username);exit; }
清除session
注意:这里我clear内不需要参数的。我这样尝试想删除一个key为user的session但是还是全部删除了,这块我自己实践是这样的,但是大家如果可以删除单个key的话希望大家传授我经验
public function sessionAction(Request $request) { $session = $request->getSession(); $sessionValue = array( array(‘username‘ => ‘gaofei‘,‘password‘ => 22222), ); $sessionValueTwo = array( array(‘username‘ => ‘gaofei‘,‘password‘ => 11111111), ); $session->set(‘user‘, $sessionValue ); $session->set(‘userinfo‘, $sessionValueTwo); $session->clear(‘user‘); $testTwo = $session->get(‘userinfo‘); var_dump($testTwo,$testTwo);exit; }
cookies的一些操作
symfony3系列的操作(都放在一起了)
public function cookieAction(Request $request) { $response = new Response(); $name = ‘test‘; $value = ‘123‘; $expire = ‘1000‘; $nametwo = ‘www‘; $response->headers->setCookie(new Cookie($name, $value, time() + $expire)); $response->headers->setCookie(new Cookie($nametwo, $value, time() + $expire)); $response->send(); // 包括 sendHeaders()、sendContent() $test = $request->cookies->get(‘www‘); var_dump($test); }
symfony2的操作
set cookies
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Cookie; public function testSetCookie($name, $value, $expire=0){ $response = new Response(); $response->headers->setCookie(new Cookie($name, $value, time() + $expire)); $response->send(); // 包括 sendHeaders()、sendContent() }
Get Cookie
public function testGetCookie() { $request = $this->getRequest(); return $request->cookies->all(); }
Clear Cookie
public function testClearCookie() { $response = new Response(); $response->headers->setCookie(new Cookie($name, $value, -1)); $response->send(); }
以上是关于symfony2 symofny3中得到get post session cookies的方法的主要内容,如果未能解决你的问题,请参考以下文章
最佳实践以及如何在 Symfony2 中找到从 iOS AFNetworking 获取 POST 数据并在 GET 中返回 JSON?
Symfony2:找不到“GET /lucky/number”的路由
Symfony2:将“pre-set”和“post-get”实体方法放在哪里?
Symfony2 测试中的伪造身份验证 - 控制器上的 get('security.context')->getToken() 返回的令牌与 TestCase 中设置的令牌不同