From 7b94a6daa9119163420ffbc654491e30e736813e Mon Sep 17 00:00:00 2001 From: Sergei Gulin Date: Wed, 17 Sep 2014 12:42:59 +0400 Subject: [PATCH 1/2] add urls --- README.md | 7 +++++++ Sitemap.php | 3 +++ controllers/DefaultController.php | 2 +- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5968524..925e0a5 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,13 @@ to the require section of your application's `composer.json` file. 'app\modules\news\models\News', ... ], + 'urls'=> [ + [ + 'loc'=>'/news/index', + 'changefreq' => SitemapBehavior::CHANGEFREQ_DAILY, + 'priority' => 0.8 + ], + ], 'enableGzip' => true, ], ... diff --git a/Sitemap.php b/Sitemap.php index e96d068..158a1c1 100644 --- a/Sitemap.php +++ b/Sitemap.php @@ -31,4 +31,7 @@ class Sitemap extends Module /** @var array $models */ public $models = []; + + /** @var array $urls */ + public $urls = []; } diff --git a/controllers/DefaultController.php b/controllers/DefaultController.php index abf2f65..bb99b3d 100644 --- a/controllers/DefaultController.php +++ b/controllers/DefaultController.php @@ -22,7 +22,7 @@ public function actionIndex() $module = $this->module; if (!$sitemapData = Yii::$app->cache->get($module->cacheKey)) { - $urls = []; + $urls = $module->urls; foreach ($module->models as $modelName) { /** @var \himiklab\sitemap\behaviors\SitemapBehavior $model */ $model = new $modelName; From edb2a2b0df23106c2c0bdc8379f99a2bd9906486 Mon Sep 17 00:00:00 2001 From: Sergei Gulin Date: Wed, 17 Sep 2014 13:47:47 +0400 Subject: [PATCH 2/2] lastmod is optional --- behaviors/SitemapBehavior.php | 8 +++++--- views/default/index.php | 4 +++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/behaviors/SitemapBehavior.php b/behaviors/SitemapBehavior.php index 5162ae9..08db725 100644 --- a/behaviors/SitemapBehavior.php +++ b/behaviors/SitemapBehavior.php @@ -81,12 +81,14 @@ public function generateSiteMap() continue; } - if (!isset($urlData['loc'], $urlData['lastmod'])) { - throw new InvalidConfigException('Params `loc` and/or `lastmod` isn`t set.'); + if (!isset($urlData['loc'])) { + throw new InvalidConfigException('Params `loc` isn`t set.'); } $result[$n]['loc'] = $urlData['loc']; - $result[$n]['lastmod'] = date(DATE_W3C, $urlData['lastmod']); + if(isset($urlData['lastmod'])) { + $result[$n]['lastmod'] = date(DATE_W3C, $urlData['lastmod']); + } $result[$n]['changefreq'] = isset($urlData['changefreq']) ? $urlData['changefreq'] : $this->defaultChangefreq; diff --git a/views/default/index.php b/views/default/index.php index b859bca..194ca3f 100644 --- a/views/default/index.php +++ b/views/default/index.php @@ -13,7 +13,9 @@ - + + +