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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!