码迷,mamicode.com
首页 > 其他好文 > 详细

symfony2 symofny3中得到get post session cookies的方法

时间:2016-05-17 19:12:50      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

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的方法

标签:

原文地址:http://www.cnblogs.com/g825482785/p/sf3.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!