コード例 #1
0
 /**
  * @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;
 }
コード例 #2
0
 /**
  * @return string|\yii\web\Response
  * @throws \yii\web\HttpException
  */
 public function actionIndex()
 {
     $model_url = new NixShortUrls();
     //save url
     if ($model_url->load(Yii::$app->request->post())) {
         if ($model_url->validate()) {
             $model_url->checkUrl($model_url['long_url']);
             $model_url->setAttributes(['short_code' => $model_url->genShortCode(), 'time_create' => date('Y-m-d')]);
             $model_url->save();
             return $this->refresh();
         }
     }
     //get all urls
     $query = NixShortUrls::find();
     $pagination = new Pagination(['defaultPageSize' => 10, 'totalCount' => $query->count()]);
     $short_urls = $query->addOrderBy('id DESC')->offset($pagination->offset)->limit($pagination->limit)->all();
     return $this->render('index', ['short_urls' => $short_urls, 'model_url' => $model_url, 'pagination' => $pagination]);
 }