dtebrq0245 2018-08-14 13:15
浏览 49
已采纳

PHP / laravel将项目添加到未保存的会话

yesterday i started my first laravel project. but i'm having a problem i can't understand.

I'm trying to create a shopping cart, which i keep track of using laravel's session object with my own wrapper. the code looks like this:

class SessionController extends Controller
{
 static function getSessionData($key = null, $data = null)
    {
        if($data === null)
        {
            return Session::get($key);
        }

        else
        {
            return Session::get($key, $data);
        }
    }

    static function allSessionData()
    {
        return Session::all();
    }

    static function putSessionData($key = null, $data)
    {
        if ($key === null)
        {
            Session::put($data);
        }
        else
        {
            Session::put($key, $data);
        }
    }

    static function has($key)
    {
        return Session::has($key);
    }  

Now inside my ShoppingCart class i created an $instance field, which keeps the shoppingcart data, or creates an empty shopping cart if it's not in the session yet. the code looks like this:

class ShoppingCart extends Model
{
    public $id;
    public $session;
    private $sessionName = 'shoppingcart';
    private $instance;
    public $cartItems;

    protected $connection = 'mysql2';
    protected $table = 'shoppingcart';
    protected $primaryKey = 'Id';   


    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->cartItems = [];
    }

    private function getOrCreateSession()
    {
        if(SessionController::has($this->sessionName))
        {
            $this->instance = SessionController::getSessionData($this->sessionName);
        }

        else
        {
            SessionController::putSessionData($this->sessionName, $this);
            $this->instance = SessionController::getSessionData($this->sessionName);
        }
    }

    function addCartItem($productId, $qty = 1)
    {
        $this->getOrCreateSession();
        $cartItem = $this->createCartitem($productId, $qty);
        $content = $this->getContent();

        if ($existingCartitem = $this->alreadyInCart($cartItem)) {
            $existingCartitem->qty += $cartItem->qty;
        } else {
            array_push($content, $cartItem);
        }
    }

    function createCartItem($productId, $qty)
    {
        $cartItem = CartItem::fromId($this->instance->id, $productId, $qty);
        $cartItem->associate($this->instance->id);

        return $cartItem;
    }

    private function alreadyInCart($cartItem)
    {
        $alreadyInCart = FALSE;

        foreach ($this->instance->cartItems as $item) {
            if ($item->productId == $cartItem->productId) {
                return $item;
            }
        }

        return $alreadyInCart;
    }

    //returns current shoppingcart contents.
    private function getContent()
    {
       return $this->instance->cartItems;
    }
}

Now inside the addCartItem-method i try to push the new cartitem into the array using array_push()but var_dump()-ing the session afterwards shows the session does not contain the newly added item. However, if i var_dump() the $content before and after the array_push-method, i can see it's added.

I thought PHP would pass the session by reference but apparantly i'm missing something here.

What am i doing wrong?

Thank you in advance.

  • 写回答

2条回答 默认 最新

  • dougua9165 2018-08-14 13:22
    关注

    You will need to return the session by reference, i.e.

    static function &getSessionData($key = null, $data = null)
    

    and use it by getting its reference, like:

    $myVariable = &SessionController::getSessionData($this->sessionName);
    

    Quote from the docs:

    Returning by reference is useful when you want to use a function to find to which variable a reference should be bound. Do not use return-by-reference to increase performance. The engine will automatically optimize this on its own. Only return references when you have a valid technical reason to do so. To return references, use this syntax:

    <?php
    class foo {
        public $value = 42;
    
        public function &getValue() {
            return $this->value;
        }
    }
    
    $obj = new foo;
    $myValue = &$obj->getValue(); // $myValue is a reference to $obj->value, which is 42.
    $obj->value = 2;
    echo $myValue;                // prints the new value of $obj->value, i.e. 2.
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里