Store objects in sessions Symfony 2

后端 未结 3 1328
终归单人心
终归单人心 2021-02-07 23:58

I am writing a small e-shop application with Symfony 2 and I need some way to store the user\'s shopping cart in a session. I think using a database is not a good idea.

3条回答
  •  臣服心动
    2021-02-08 00:15

    Don't know if this way is the better way to store your data temporary. You can use this to instantiate a session object :

    $session = $this->get("session");

    Don't forget the 'use' in your controller :

    use Symfony\Component\HttpFoundation\Session;
    

    Then, the session starts automatically when you want to set a variable like :

    $session->set("product name","computer");

    This is based on the use of the Session class, easy to understand. Commonly definitions :

    get(string $name, mixed $default = null)
    
    Returns an attribute.
    
    set(string $name, mixed $value)
    
    Sets an attribute.
    
    has(string $name)
    
    Checks if an attribute is defined.
    

    Also, take a look to the other ways to store your data : Multiple SessionStorage

提交回复
热议问题