/** * @param $code * @return \yii\web\Response * @throws \yii\web\HttpException * @throws \yii\web\NotAcceptableHttpException * @throws \yii\web\NotFoundHttpException */ public function actionForward($code) { $model_info = new NixUserInfo(); $url = NixShortUrls::validateShortCode($code); $url->updateCounters(['counter' => 1]); $model_info->setAttributes(['short_url_id' => $url['id'], 'user_agent' => Yii::$app->request->userAgent, 'user_refer' => Yii::$app->request->referrer, 'user_ip' => Yii::$app->request->userIP, 'date' => date('Y-m-d')]); $model_info->save(); return $this->redirect($url['long_url']); }
/** * @param $code * @return array|null|\yii\db\ActiveRecord * @throws HttpException * @throws NotAcceptableHttpException * @throws NotFoundHttpException */ public static function validateShortCode($code) { if (!preg_match('|^[0-9a-zA-Z]{6,6}$|', $code)) { throw new HttpException(400, 'Please enter valid short code'); } $url = NixShortUrls::find()->where(['short_code' => $code])->one(); if ($url === null) { throw new NotFoundHttpException('This short code not found:' . $code); } else { if (!is_null($url['time_end']) && date('Y-m-d') > $url['time_end']) { throw new NotAcceptableHttpException('This short code was disabled by the end time ' . $url['time_end']); } } return $url; }