Read and Write Cookie in Symfony2

前端 未结 6 442
無奈伤痛
無奈伤痛 2020-12-12 18:24

I want to store some information in the local browser cookie. After hours looking for a nice tutorial, I managed to store some data in a non-session cookie:

相关标签:
6条回答
  • 2020-12-12 18:51
    $response->headers->getCookies();
    

    should return an array of cookies look in ResponseHeaderBag class for more information about that function

    0 讨论(0)
  • 2020-12-12 18:52
                use Symfony\Component\HttpFoundation\Cookie;
                use Symfony\Component\HttpFoundation\Response;
    
                // set current active tab in cookie 
                $cookie = new Cookie('myProfileActiveTab', 'myaddress', strtotime('now + 60 minutes'));
                $response = new Response();
                $response->headers->setCookie($cookie);
                $response->send();
    
    
               // get current active tab from cookies
               $cookies = $request->cookies;
               if ($cookies->has('myProfileActiveTab')) {
                   $activetab = $cookies->get('myProfileActiveTab');
               } 
    
    0 讨论(0)
  • 2020-12-12 19:00

    More interesting cookies information links (http_fundation component for symfony2):

    Symfony2 http_fundation component

    Symfony2 http_fundation api

    Symfony2 http_fundation component (spanish)

    0 讨论(0)
  • 2020-12-12 19:02

    This is the correct way of setting cookie. To read cookie already written in the browser do:

    $request->cookies->get('myCookie');
    

    But after I created cookie in the $response object:

    $cookie = new Cookie('myCookie', 'contentOfMyCookie');
    $response = new Response();
    $response->headers->setCookie($cookie);
    

    I call this method:

    $response->headers->getCookies();
    

    I get an array of cookies, which are to be written in the browser - not those already existing there.

    Figuratively, between $request and $response there is a time of executing controller's code.

    Besides, in a twig template you can use

    {{ app.request.cookies.get('myCookie') }}
    

    you thus get value of the cookie already written in the browser, not that from the $response object! Newly created cookie from the browser you can read only after having reloaded page (ajax doesn't need to reload whole page).

    To sum it up, you can read cookies using $request object, and create them with $response object. (Obviously, for some reasons, you can also read $response object cookies - but these are rather rare situations).

    0 讨论(0)
  • 2020-12-12 19:05

    Example how to use Cookies and Session:

    <?php
    
    namespace AppBundle\Controller;
    
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Cookie;
    use Symfony\Component\HttpFoundation\Response;
    
    class DefaultController extends Controller
    {
        public function indexAction()
        {
            // Set session value
            $session = $this->getRequest()->getSession();
            $session->set('test', 1);
    
            // Get session value
            $value = $session->get('test');
    
            // Set cookie value
            $response = new Response();
            $cookie = new Cookie('test', 1, time()+3600);
            $response->headers->setCookie($cookie);
    
            // Get cookie value
            $this->getRequest()->cookies->get('test');
        }
    }
    
    0 讨论(0)
  • 2020-12-12 19:07

    this can be useful for someone trying to make cookies in symfony2 :

    use Symfony\Component\HttpFoundation\Cookie;
    
    0 讨论(0)
提交回复
热议问题