namespace app\models; use yii\db\ActiveRecord; class Cart extends ActiveRecord { public function addToCart($product, $qty = 1) { if ($_SESSION['cart'][$product->id]) { $_SESSION['cart'][$product->id]['qty'] += $qty; } else { $_SESSION['cart'][$product->id] = [ 'qty' => $qty, 'name' => $product->name, 'slug' => $product->slug, 'price' => $product->price, 'img' => $product->getImage()->getUrl('80x') ];
class Cart extends ActiveRecord { public function addToCart($product, $qty = 1) { if ($_SESSION['cart'][$product->id]) { $_SESSION['cart'][$product->id]['qty'] += $qty; } else { $_SESSION['cart'][$product->id] = [ 'qty' => $qty, 'name' => $product->name, 'slug' => $product->slug,
$qty = !$qty ? 1 : $qty; $product = Product::findOne($id); if (empty($product)) return false; $session = Yii::$app->session; $session->open(); $cart = new Cart(); $cart->addToCart($product, $qty); if (Yii::$app->request->isAjax) return $session['cart.qty'] ? $session['cart.qty'] : '0'; return $this->redirect(Yii::$app->request->referrer); } public function actionShow()
$args = $this->controller->bindActionParams($this, $params); Yii::trace('Running action: ' . get_class($this->controller) . '::' . $this->actionMethod . '()', __METHOD__); if (Yii::$app->requestedParams === null) { Yii::$app->requestedParams = $args; } return call_user_func_array([$this->controller, $this->actionMethod], $args); } }
} $result = null; if ($runAction && $this->beforeAction($action)) { // run the action $result = $action->runWithParams($params); $result = $this->afterAction($action, $result); // call afterAction on modules foreach ($modules as $module) { /* @var $module Module */
$parts = $this->createController($route); if (is_array($parts)) { /* @var $controller Controller */ list($controller, $actionID) = $parts; $oldController = Yii::$app->controller; Yii::$app->controller = $controller; $result = $controller->runAction($actionID, $params); if ($oldController !== null) { Yii::$app->controller = $oldController; } return $result; }
$params = $this->catchAll; unset($params[0]); } try { Yii::trace("Route requested: '$route'", __METHOD__); $this->requestedRoute = $route; $result = $this->runAction($route, $params); if ($result instanceof Response) { return $result; } else { $response = $this->getResponse(); if ($result !== null) { $response->data = $result;
try { $this->state = self::STATE_BEFORE_REQUEST; $this->trigger(self::EVENT_BEFORE_REQUEST); $this->state = self::STATE_HANDLING_REQUEST; $response = $this->handleRequest($this->getRequest()); $this->state = self::STATE_AFTER_REQUEST; $this->trigger(self::EVENT_AFTER_REQUEST); $this->state = self::STATE_SENDING_RESPONSE; $response->send();
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php'); require(__DIR__ . '/../functions.php'); $config = require(__DIR__ . '/../config/web.php'); (new yii\web\Application($config))->run();
$_GET = [ 'id' => '2334', ]; $_SESSION = [ '__flash' => [], ];