L
L
lzy2018-12-05 09:33:53
Yii
lzy, 2018-12-05 09:33:53

How to pass value from one action to another in Yii2?

Good afternoon. I can’t transfer the order number of the Basket from one action to another, that is, in the basket a person makes an order, the data is saved to the database, and then the Success page of this order should be opened. How to do it? I attach actionView and actionSuccess()
PS: You need to write redirect correctly

public function actionView(){
        $session = Yii::$app->session;
        $session->open();
        $this->setMeta('Корзина');
        $order = new Order();
        if($order->load(Yii::$app->request->post())){
            $order->qty = $session['cart.qty'];
            $order->sum = $session['cart.sum'];

            if($order->save()){
                $this->saveOrderItems($session['cart'], $order->id);
                Yii::$app->session->setFlash('Success', 'Ваш заказ принят.');
                //Send message
                Yii::$app->mailer->compose('order', ['session'=>$session])
                    ->setFrom(['[email protected]' => 'yii2.loc'])
                    ->setTo($order->email)
                    ->setSubject('Заказ с сайта')
                    ->send();

                Yii::$app->mailer->compose('order', ['session'=>$session])
                    ->setFrom(['[email protected]' => 'yii2.loc'])
                    ->setTo(Yii::$app->params['adminEmail'])
                    ->setSubject('Заказ с сайта')
                    ->send();

                $session->remove('cart');
                $session->remove('cart.qty');
                $session->remove('cart.sum');
                return $this->refresh();
            }
            else{
                Yii::$app->session->setFlash('Error', 'Ошибка');
            }
        }
       //Как перекинуть человека на Success page с данным ордером id???
        return $this->render('view', compact('session', 'order'));
    }

protected function saveOrderItems($items, $order_id){
        foreach ($items as $id=> $item){
            $order_items = new OrderItems();
            $order_items->order_id = $order_id;
            $order_items->product_id = $id;
            $order_items->name = $item['name'];
            $order_items->price = $item['price'];
            $order_items->qty_item = $item['qty'];
            $order_items->sum_item = $item['price'] * $item['qty'];
            $order_items->save();
        }
    }

public function actionSuccess($id) {
        $session = Yii::$app->session;

        $model = Order::findOne($id);
        return $this->render('success');
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-12-05
@lzy

Good morning. Do
insteadreturn $this->refresh();return $this->redirect('path_to_action')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question