#1097 - Создать Sitemap специально для яндекс

parent 8c62d8cc
...@@ -11,6 +11,7 @@ use common\modules\blog\models\PostTag; ...@@ -11,6 +11,7 @@ use common\modules\blog\models\PostTag;
use common\modules\users\models\User; use common\modules\users\models\User;
use common\models\MetaTags; use common\models\MetaTags;
use common\modules\sessions\models\SessionUrl; use common\modules\sessions\models\SessionUrl;
use frontend\modules\sitemap\behaviors\SitemapBehavior;
/** /**
* This is the model class for table "posts". * This is the model class for table "posts".
...@@ -95,6 +96,22 @@ class Post extends \common\components\ActiveRecordModel ...@@ -95,6 +96,22 @@ class Post extends \common\components\ActiveRecordModel
'folder' => '/uploads/blog/', 'folder' => '/uploads/blog/',
'field' => 'preview' 'field' => 'preview'
], ],
'sitemap' => [
'class' => SitemapBehavior::className(),
'scope' => function ($model) {
/** @var \yii\db\ActiveQuery $model */
$model->select(['url', 'updated_at']);
$model->andWhere(['active' => 1]);
},
'dataClosure' => function ($model) {
return [
'loc' => Url::to('blog/'.$model->url),
'lastmod' => date('c', $model->updated_at),
'changefreq' => SitemapBehavior::CHANGEFREQ_DAILY,
'priority' => 0.5
];
}
],
]; ];
} }
......
...@@ -31,6 +31,7 @@ return [ ...@@ -31,6 +31,7 @@ return [
'models' => [ 'models' => [
// your models // your models
'common\modules\content\models\CoContent', 'common\modules\content\models\CoContent',
'common\modules\blog\models\Post',
], ],
'urls'=> [ 'urls'=> [
// // your additional urls // // your additional urls
......
...@@ -59,7 +59,7 @@ class Sitemap extends Module ...@@ -59,7 +59,7 @@ class Sitemap extends Module
* @return string * @return string
* @throws \yii\base\InvalidConfigException * @throws \yii\base\InvalidConfigException
*/ */
public function buildSitemap($links = false) public function buildSitemap($google = false)
{ {
$urls = $this->urls; $urls = $this->urls;
...@@ -86,7 +86,7 @@ class Sitemap extends Module ...@@ -86,7 +86,7 @@ class Sitemap extends Module
$sitemapData = $this->createControllerByID('default')->renderPartial('index', [ $sitemapData = $this->createControllerByID('default')->renderPartial('index', [
'urls' => $urls, 'urls' => $urls,
'langs' => $langs, 'langs' => $langs,
'links' => $links 'google' => $google
]); ]);
$this->cacheProvider->set($this->cacheKey, $sitemapData, $this->cacheExpire); $this->cacheProvider->set($this->cacheKey, $sitemapData, $this->cacheExpire);
......
...@@ -17,7 +17,7 @@ echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL; ...@@ -17,7 +17,7 @@ echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
<url> <url>
<loc><?= yii\helpers\Url::to($url['loc'], true) ?></loc> <loc><?= yii\helpers\Url::to($url['loc'], true) ?></loc>
<?php if($links && $langs) : <?php if($google && $langs) :
foreach ($langs as $lang) : ?> foreach ($langs as $lang) : ?>
<xhtml:link rel="alternate" hreflang="<?=$lang->url?>" href="<?= yii\helpers\Url::to((!$lang->default?'/'.$lang->url:'').($url['loc']=='/'?'':'/'.$url['loc']), true) ?>"/> <xhtml:link rel="alternate" hreflang="<?=$lang->url?>" href="<?= yii\helpers\Url::to((!$lang->default?'/'.$lang->url:'').($url['loc']=='/'?'':'/'.$url['loc']), true) ?>"/>
<?php endforeach; <?php endforeach;
...@@ -74,5 +74,25 @@ echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL; ...@@ -74,5 +74,25 @@ echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
<?php endforeach; <?php endforeach;
endif; ?> endif; ?>
</url> </url>
<?php if(!$google && $langs) :
foreach ($langs as $lang) :
if(!$lang->default) : ?>
<url>
<loc><?= yii\helpers\Url::to('/'.$lang->url.($url['loc']=='/'?'':'/'.$url['loc']), true) ?></loc>
<?php if (isset($url['lastmod'])): ?>
<lastmod><?= is_string($url['lastmod']) ?
$url['lastmod'] : date(DATE_W3C, $url['lastmod']) ?></lastmod>
<?php endif; ?>
<?php if (isset($url['changefreq'])): ?>
<changefreq><?= $url['changefreq'] ?></changefreq>
<?php endif; ?>
<?php if (isset($url['priority'])): ?>
<priority><?= $url['priority'] ?></priority>
<?php endif; ?>
</url>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?> <?php endforeach; ?>
</urlset> </urlset>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment