├── LICENSE.md ├── README.md ├── Sitemap.php ├── SitemapController.php ├── SitemapInterface.php └── composer.json /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2021 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Sitemap Extension for Yii 2 2 | =========================== 3 | 4 | [![Latest Stable Version](https://img.shields.io/packagist/v/mrssoft/yii2-sitemap.svg)](https://packagist.org/packages/mrssoft/yii2-sitemap) 5 | ![PHP](https://img.shields.io/packagist/php-v/mrssoft/yii2-sitemap.svg) 6 | ![Github](https://img.shields.io/github/license/mrs2000/yii2-sitemap.svg) 7 | ![Total Downloads](https://img.shields.io/packagist/dt/mrssoft/yii2-sitemap.svg) 8 | 9 | Installation 10 | ------------ 11 | 12 | The preferred way to install this extension is through [composer](http://getcomposer.org/download/). 13 | 14 | Either run 15 | 16 | ``` 17 | php composer.phar require mrssoft/yii2-sitemap "^2.0" 18 | ``` 19 | 20 | or add 21 | 22 | ``` 23 | "mrssoft/yii2-sitemap": "^2.0" 24 | ``` 25 | 26 | to the require section of your composer.json. 27 | 28 | 29 | Usage 30 | ----- 31 | 32 | Create controller `SitemapController.php` 33 | 34 | ```php 35 | \app\models\Page::class, 62 | 'change' => Sitemap::MONTHLY, 63 | 'priority' => 0.8, 64 | 'lastmod' => 'updated_at', 65 | ] 66 | ]; 67 | } 68 | 69 | public function urls(): array 70 | { 71 | return [ 72 | [ 73 | 'url' => ['about/index'], 74 | 'priority' => 0.8 75 | ] 76 | ]; 77 | } 78 | } 79 | ``` 80 | 81 | Add to your models interface `\mrssoft\sitemap\SitemapInterface` 82 | 83 | ```php 84 | where(['public' => 1]); 97 | } 98 | 99 | /** 100 | * @return string 101 | */ 102 | public function getSitemapUrl(): string 103 | { 104 | return \yii\helpers\Url::toRoute(['page/view', 'url' => $this->url], true); 105 | } 106 | } 107 | ``` 108 | 109 | Add to config url rule. 110 | 111 | ```php 112 | 'components' => [ 113 | 'urlManager' => [ 114 | 'rules' => [ 115 | ... 116 | [ 117 | 'pattern' => 'sitemap', 118 | 'route' => 'sitemap/index', 119 | 'suffix' => '.xml' 120 | ], 121 | ... 122 | ] 123 | ], 124 | ``` 125 | -------------------------------------------------------------------------------- /Sitemap.php: -------------------------------------------------------------------------------- 1 | items)) { 30 | return; 31 | } 32 | 33 | $item = [ 34 | 'loc' => $url, 35 | 'changefreq' => $changeFreq, 36 | 'priority' => $priority 37 | ]; 38 | 39 | if ($lastMod) { 40 | $item['lastmod'] = $this->dateToW3C($lastMod); 41 | } 42 | 43 | $this->items[$url] = $item; 44 | } 45 | 46 | /** 47 | * @param SitemapInterface[]|\yii\db\ActiveRecord $models 48 | * @param string $changeFreq 49 | * @param float $priority 50 | * @param string $lastmod 51 | */ 52 | 53 | public function addModels(array $models, string $changeFreq, float $priority, string $lastmod): void 54 | { 55 | foreach ($models as $model) { 56 | $url = $model->getSitemapUrl(); 57 | if (in_array($url, $this->items) === false) { 58 | $item = [ 59 | 'loc' => $url, 60 | 'changefreq' => $changeFreq, 61 | 'priority' => $priority 62 | ]; 63 | 64 | if ($model->hasAttribute($lastmod)) { 65 | $item['lastmod'] = $this->dateToW3C($model->getAttribute($lastmod)); 66 | } 67 | 68 | $this->items[$url] = $item; 69 | } 70 | } 71 | } 72 | 73 | /** 74 | * @return string XML code 75 | */ 76 | public function render(bool $enablePriority, bool $enablechangeFreq): string 77 | { 78 | $dom = new \DOMDocument('1.0', 'utf-8'); 79 | $urlset = $dom->createElement('urlset'); 80 | $urlset->setAttribute('xmlns', 'https://www.sitemaps.org/schemas/sitemap/0.9'); 81 | foreach ($this->items as $item) { 82 | $url = $dom->createElement('url'); 83 | 84 | foreach ($item as $key => $value) { 85 | if ($key === 'priority' && $enablePriority === false) { 86 | continue; 87 | } 88 | if ($key === 'changefreq' && $enablechangeFreq === false) { 89 | continue; 90 | } 91 | $elem = $dom->createElement($key); 92 | $elem->appendChild($dom->createTextNode($value)); 93 | $url->appendChild($elem); 94 | } 95 | 96 | $urlset->appendChild($url); 97 | } 98 | $dom->appendChild($urlset); 99 | 100 | return $dom->saveXML(); 101 | } 102 | 103 | protected function dateToW3C($date): string 104 | { 105 | if (is_int($date)) { 106 | return date(DATE_W3C, $date); 107 | } 108 | 109 | return date(DATE_W3C, strtotime($date)); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /SitemapController.php: -------------------------------------------------------------------------------- 1 | runtimePath . DIRECTORY_SEPARATOR . $this->cacheFilename; 46 | 47 | if ($this->cacheDuration === null || is_file($cachePath) === false || filemtime($cachePath) < time() - $this->cacheDuration) { 48 | $xml = $this->generateXml(); 49 | if ($this->cacheDuration) { 50 | file_put_contents($cachePath, $xml); 51 | } 52 | } else { 53 | $xml = file_get_contents($cachePath); 54 | } 55 | 56 | Yii::$app->response->format = Response::FORMAT_RAW; 57 | Yii::$app->getResponse() 58 | ->getHeaders() 59 | ->set('Content-Type', 'text/xml; charset=utf-8'); 60 | 61 | return $xml; 62 | } 63 | 64 | private function generateXml(): string 65 | { 66 | $sitemap = new Sitemap(); 67 | 68 | foreach ($this->urls() as $item) { 69 | $sitemap->addUrl( 70 | isset($item['url']) ? Url::toRoute($item['url'], true) : Url::toRoute($item, true), 71 | $item['change'] ?? Sitemap::DAILY, 72 | $item['priority'] ?? Sitemap::DEFAULT_PRIORITY, 73 | $item['lastmod'] ?? 0 74 | ); 75 | } 76 | 77 | foreach ($this->models() as $model) { 78 | $obj = new $model['class']; 79 | if ($obj instanceof SitemapInterface) { 80 | $models = $obj::sitemap() 81 | ->all(); 82 | $sitemap->addModels( 83 | $models, 84 | $model['change'] ?? Sitemap::DAILY, 85 | $model['priority'] ?? Sitemap::DEFAULT_PRIORITY, 86 | $model['lastmod'] ?? Sitemap::LASTMOD_FIELD 87 | ); 88 | } 89 | } 90 | 91 | return $sitemap->render($this->enablePriority, $this->enableChangeFreq); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /SitemapInterface.php: -------------------------------------------------------------------------------- 1 | =7.1", 20 | "yiisoft/yii2": "*" 21 | }, 22 | "autoload": { 23 | "psr-4": { 24 | "mrssoft\\sitemap\\": "" 25 | } 26 | } 27 | } --------------------------------------------------------------------------------