Skip to content
This repository has been archived by the owner on Jan 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1 from SergeiGulin/master
Browse files Browse the repository at this point in the history
add urls + lastmod is optional
  • Loading branch information
himiklab committed Sep 17, 2014
2 parents 874a442 + edb2a2b commit 3a840e9
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
...
Expand Down
3 changes: 3 additions & 0 deletions Sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ class Sitemap extends Module

/** @var array $models */
public $models = [];

/** @var array $urls */
public $urls = [];
}
8 changes: 5 additions & 3 deletions behaviors/SitemapBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion views/default/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
<?php foreach ($urls as $url): ?>
<url>
<loc><?= $url['loc'] ?></loc>
<lastmod><?= $url['lastmod'] ?></lastmod>
<?php if(isset($url['lastmod'])): ?>
<lastmod><?= $url['lastmod'] ?></lastmod>
<?php endif; ?>
<changefreq><?= $url['changefreq'] ?></changefreq>
<priority><?= $url['priority'] ?></priority>
</url>
Expand Down

0 comments on commit 3a840e9

Please sign in to comment.