奇怪的错误会话被破坏

时间:2016-12-27 07:08:53

标签: session yii2

我执行控制器方法时遇到问题,它处理控制器代码但会破坏包括用户在内的所有会话。

因此,在控制器和我尝试打开主页之后,没有登录帐户。

我确定在我的控制器中没有销毁会话代码或注销代码。我可以说这不是关于会话过期的。

我不知道代码,yii2配置或逻辑有什么问题,但过去有人有类似问题吗?

希望你能指导我解决这个问题。

如果您需要更多信息,请告诉我们。

这可能与代码无关,但这是我的控制器代码。

<?php
namespace frontend\controllers;

use Yii;
use common\models\TbCustomer;
use yii\web\Controller;
use common\models\Model;
use common\models\VwProdukAgent;
use common\models\TbCart;
use yii\helpers\VarDumper;
use common\models\VwProduk;
use yii\data\ActiveDataProvider;
use common\models\VwProdukCustomer;
use common\models\TbCustomerShipment;
use common\component\BeoHelper;
use common\models\TbProdukEkspedisi;
use common\models\TbKota;
use yii\helpers\Url;


/**
 * CustomerController implements the CRUD actions for TbCustomer model.
 */
class Pengiriman1Controller extends Controller
{
    public function beforeAction($action)
    {
        Yii::$app->timeZone = 'Asia/Jakarta';
        if(Yii::$app->user->isGuest)
        {
            //$url = Url::to([['home'],'message'=>'Anda Harus Login Terlebih Dahulu']);
            \Yii::$app->getSession()->setFlash('message', \Yii::t('app', 'Anda Harus Login Terlebih Dahulu'));
            $this->goHome();
            return FALSE;
        }

        //if ($action->id == 'my-method') {
        $this->enableCsrfValidation = false;
        //  }
        return parent::beforeAction($action);
    }

    public function actionIndex()
    {
        //get alamat customer
        //$lsalamat = array();
        if(!\Yii::$app->user->isGuest)
        {


            BeoHelper::refreshCart();

            //cek cart kosong atau tidak
            if(isset(\Yii::$app->session['produkcart']))
            {
                $lsproduk = \Yii::$app->session['produkcart'];
                if (count($lsproduk)<1)
                {
                    \Yii::$app->getSession()->setFlash('message',"silahkan berbelanja terlebih dahulu");
                    return $this->redirect(Url::to(['cart/index']));
                }
            }

            $customer_id = Yii::$app->user->id; 
            $message = '';
            $model = null;
            if(\Yii::$app->request->post('opsi_alamat',null)!=null)
            {
                $opsi_pengiriman = \Yii::$app->request->post('opsi_alamat',null);
                //return $opsi_pengiriman;
                if($opsi_pengiriman == 'new')
                {
                    $model = new TbCustomerShipment();
                    $model->load(Yii::$app->request->post());
                    $model->customer_id = $customer_id;
                    if($model->save())
                    {
                        \Yii::$app->session['shipemenadd'] = $model;
                        //get kurir preselected
                        if(isset(\Yii::$app->session['produkcart']))
                        {
                            $hargatotal = 0;
                            $lsproduk = \Yii::$app->session['produkcart'];
                            $i=0;
                            $lsprodukedit = array();
                            foreach ($lsproduk as $produk)
                            {
                                //pre selected
                                $pengiriman = TbProdukEkspedisi::find()->where(['produk_id' => $produk->produk_id])->one();
                                $kode = $pengiriman->idEkspedisi->kode_ekspedisi;
                                $service = $pengiriman->idEkspedisi->service;
                                $produk->kurir = $kode;
                                $kota_asal = TbKota::find()->where("kota_id = $produk->kota_id ")->one();
                                $kota_tujuan = TbKota::find()->where("kota_id = $model->kota_id ")->one();

                                $berat_produk = $produk->kuantitas *$produk->berat_produk;
                                $berat_produk = round($berat_produk);
                                $result = BeoHelper::getCostEkspedisi($kota_asal->api_id, $kota_tujuan->api_id, $berat_produk, $kode,$service,$produk->produk_id);
                                if(count($result)!=0)
                                {

                                    $produk->harga_kurir = $result[0]['value'];
                                    $produk->estimasi_sampai = $result[0]['etd'];
                                }
                                else 
                                {
                                    $message = "pilihan kurir untuk lokasi tersebut tidak tersedia";
                                }
                                //return VarDumper::dump($result);

                                $lsprodukedit[$i++] = $produk;
                                $hargatotal += $produk->kuantitas * $produk->harga_agen;


                            }
                            \Yii::$app->session['produkcart'] = $lsprodukedit;
                        }   
                        if(isset(Yii::$app->session['shipemenadd'])){
                            return "exists";        
                        }else{
                            return "doesn't exist";
                        }
                        //return $this->redirect(['pengiriman2/index','message'=>$message]);
                        //print_r(Yii::$app->session['shipemenadd']);
                    }
                    //else 
                    //{
                    //  VarDumper::dump($model);
                    //}
                }
                else 
                {
                    $pengiriman_selected = TbCustomerShipment::find()->where("customer_shipment_id = $opsi_pengiriman")->one();
                    \Yii::$app->session['shipemenadd'] = $pengiriman_selected;

                    if(isset(\Yii::$app->session['produkcart']))
                    {
                        $hargatotal = 0;
                        $lsproduk = \Yii::$app->session['produkcart'];
                        $i=0;
                        $lsprodukedit = array();
                        foreach ($lsproduk as $produk)
                        {
                            //pre selected
                            $pengiriman = TbProdukEkspedisi::find()->where("produk_id =$produk->produk_id")->one();
                            $kode = $pengiriman->idEkspedisi->kode_ekspedisi;
                            $service = $pengiriman->idEkspedisi->service;
                            $produk->kurir = $kode;
                            //get kota id rajaongkir
                            $kota_asal = TbKota::find()->where("kota_id = $produk->kota_id ")->one();
                            $kota_tujuan = TbKota::find()->where("kota_id = $pengiriman_selected->kota_id ")->one();

                            //$result = BeoHelper::getCostEkspedisi($kota_asal->api_id, $kota_tujuan->api_id, $produk->berat_produk, $kode,$service);
                            //return VarDumper::dump($result);
                            //if(count($result)!=0)
                            //{

                                //$produk->harga_kurir = $result[0]['value'];
                                //$produk->estimasi_sampai = $result[0]['etd'];
                                $produk->harga_kurir = 0;
                                $produk->estimasi_sampai = 0;
                            //}
                            //else 
                            //{
                            //  $message = "pilihan kuriri untuk lokasi tersebut tidak tersedia";
                            //}
                            $lsprodukedit[$i++] = $produk;
                            $hargatotal += $produk->kuantitas * $produk->harga_agen;
                        }
                        \Yii::$app->session['produkcart'] = $lsprodukedit;
                    }

                    return $this->redirect(['pengiriman2/index','message'=>$message]);
                }
            }

            $lsalamat = TbCustomerShipment::find()->where(['customer_id'=>$customer_id])->all();
            if(count($lsalamat)==0)
            //insert to tb shipment
            {
                $datacustomer = TbCustomer::find()->where(['customer_id'=>$customer_id])->one();
                $newalamat = new TbCustomerShipment();
                $newalamat->customer_id = $customer_id;
                $newalamat->kota_id = $datacustomer->kota_id;
                $newalamat->kecamatan_id = $datacustomer->kecamatan_id;
                $newalamat->negara_id = $datacustomer->negara_id;
                $newalamat->propinsi_id = $datacustomer->propinsi_id;
                $newalamat->alamat = $datacustomer->alamat;
                $newalamat->shipment_name = "Alamat Rumah";
                $newalamat->penerima = $datacustomer->nama;
                $newalamat->hp_penerima = $datacustomer->hp;
                $newalamat->save();
            }
            $lsalamat = TbCustomerShipment::find()->where(['customer_id'=>$customer_id])->all();
            return $this->render('index',['lsalamat'=>$lsalamat,'newalamat'=> $model]);
        }
        else 
        {
            return $this->goHome();
        }


    }
}

1 个答案:

答案 0 :(得分:0)

这是问题

\Yii::$app->session['shipemenadd'] = $model;

它应该

$currentTbCustomerShipment = TbCustomerShipment::find()->where(['customer_user_id' => $model->customer_user_id])->one(); //find latest object
Yii::$app->session['shipemenadd'] = $currentTbCustomerShipment;