コード例 #1
0
ファイル: UrlRule.php プロジェクト: ftw-soft/yii2-beauty-url
 /**
  * @inheritdoc
  */
 public function createUrl($manager, $route, $params)
 {
     $queryParams = [];
     foreach ($this->queryParams as $name) {
         if (isset($params[$name])) {
             $queryParams[$name] = $params[$name];
             unset($params[$name]);
         }
     }
     // removing leading and/or trailing slashes
     $route = trim($route, '/');
     $paramsHash = UrlHelper::generateParamsHash($params);
     if ($model = $this->searchModel->findOne(['category_id' => $this->category, 'route' => $route, 'paramsHash' => $paramsHash])) {
         $url = $model->uri;
         if (($suffix = (string) ($this->suffix === null ? $manager->suffix : $this->suffix)) && $url) {
             $url .= $suffix;
         }
         if ($queryParams && ($queryStr = http_build_query($queryParams))) {
             $url .= '?' . $queryStr;
         }
         return $url;
     }
     return false;
 }
コード例 #2
0
ファイル: UrlModel.php プロジェクト: ftw-soft/yii2-beauty-url
 /**
  * @inheritdoc
  */
 public function beforeSave($insert)
 {
     if (parent::beforeSave($insert)) {
         $params = $this->getParams();
         $this->setAttribute('params', Json::encode($params));
         $this->setAttribute('paramsHash', UrlHelper::generateParamsHash($params));
         $this->setAttribute('uri', trim($this->getAttribute('uri'), '/'));
         return true;
     }
     return false;
 }
コード例 #3
0
 /**
  * Checks whether the new url generation is needed
  *
  * @return boolean
  */
 protected function isNewUrlNeeded()
 {
     if ($this->owner->isNewRecord || $this->getUrlModel()->isNewRecord) {
         return true;
     } elseif (!$this->immutable) {
         $paramsHash = UrlHelper::generateParamsHash($this->params);
         return $this->getUrlModel()->paramsHash !== $paramsHash || $this->generateUri() !== $this->getUrlModel()->uri;
     }
     return false;
 }