I am building a simple shopping cart app in Yii2 and whatever I try I can’t make the following work.
$session = new Session();
$session[‘cart’] = ;
Yii::$app->session[‘cart’] = $product;
I have tried storing a new Cart object inside $session[‘cart’] with a property $cart which is an array and then wrote:
Yii::$app->session[‘cart’]->cart = $product;
And this doesn’t work either because I get the following notice when I try to access the property of the object:
[i] PHP Notice – yii\base\ErrorException
app\controllers\SiteController::actionAdd(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "app\controllers\Cart" of the object you are trying to operate on was loaded before unserialize() gets called or provide a __autoload() function to load the class definition [/i]
Obviously the class definition needs to be loaded before session_start() but, how do I do that in Yii2? I have tried everything.
For now I will move forward with using $_SESSION superglobal instead.