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

parent 8c62d8cc
......@@ -11,6 +11,7 @@ use common\modules\blog\models\PostTag;
use common\modules\users\models\User;
use common\models\MetaTags;
use common\modules\sessions\models\SessionUrl;
use frontend\modules\sitemap\behaviors\SitemapBehavior;
/**
* This is the model class for table "posts".
......@@ -95,6 +96,22 @@ class Post extends \common\components\ActiveRecordModel
'folder' => '/uploads/blog/',
'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 [
'models' => [
// your models
'common\modules\content\models\CoContent',
'common\modules\blog\models\Post',
],
'urls'=> [
// // your additional urls
......
......@@ -59,7 +59,7 @@ class Sitemap extends Module
* @return string
* @throws \yii\base\InvalidConfigException
*/
public function buildSitemap($links = false)
public function buildSitemap($google = false)
{
$urls = $this->urls;
......@@ -86,7 +86,7 @@ class Sitemap extends Module
$sitemapData = $this->createControllerByID('default')->renderPartial('index', [
'urls' => $urls,
'langs' => $langs,
'links' => $links
'google' => $google
]);
$this->cacheProvider->set($this->cacheKey, $sitemapData, $this->cacheExpire);
......
......@@ -17,7 +17,7 @@ echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
<url>
<loc><?= yii\helpers\Url::to($url['loc'], true) ?></loc>
<?php if($links && $langs) :
<?php if($google && $langs) :
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) ?>"/>
<?php endforeach;
......@@ -74,5 +74,25 @@ echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
<?php endforeach;
endif; ?>
</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; ?>
</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