Exemplo n.º 1
0
 /**
  * Creates a new Favorite model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     //if(!Yii::$app->user->can('createYourAuth')) throw new ForbiddenHttpException(Yii::t('app', 'No Auth'));
     $model = new Favorite();
     $model->loadDefaultValues();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Exemplo n.º 2
0
 public function actionFavorite($id)
 {
     if (!Yii::$app->user->isGuest) {
         Yii::$app->response->format = Response::FORMAT_JSON;
         $favorite = Favorite::find()->where(['user_id' => Yii::$app->user->id, 'product_id' => $id])->one();
         if (!$favorite) {
             $model = new Favorite(['user_id' => Yii::$app->user->id, 'product_id' => $id]);
             if ($model->save()) {
                 return ['status' => 1];
             }
         }
         return ['status' => 2];
     } else {
         return $this->redirect('site/login');
     }
 }