This repository has been archived by the owner on Jan 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
136 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,109 @@ | ||
<?php | ||
/** | ||
* @link https://github.com/himiklab/yii2-sitemap-module | ||
* @copyright Copyright (c) 2014 HimikLab | ||
* @license http://opensource.org/licenses/MIT | ||
*/ | ||
|
||
namespace himiklab\sitemap\behaviors; | ||
|
||
use yii\base\Behavior; | ||
use yii\db\ActiveRecord; | ||
use yii\base\InvalidConfigException; | ||
|
||
/** | ||
* Behavior for XML Sitemap Yii2 module. | ||
* | ||
* For example: | ||
* | ||
* ```php | ||
* public function behaviors() | ||
* { | ||
* return [ | ||
* 'sitemap' => [ | ||
* 'class' => SitemapBehavior::className(), | ||
* 'dataClosure' => function ($model) { | ||
* return [ | ||
* 'loc' => Url::to(model->url, true), | ||
* 'lastmod' => strtotime($model->lastmod), | ||
* 'changefreq' => SitemapBehavior::CHANGEFREQ_DAILY, | ||
* 'priority' => 0.8 | ||
* ]; | ||
* } | ||
* ], | ||
* ]; | ||
* } | ||
* ``` | ||
* | ||
* @see http://www.sitemaps.org/protocol.html | ||
* @author HimikLab | ||
* @package himiklab\sitemap | ||
*/ | ||
class SitemapBehavior extends Behavior | ||
{ | ||
const CHANGEFREQ_ALWAYS = 'always'; | ||
const CHANGEFREQ_HOURLY = 'hourly'; | ||
const CHANGEFREQ_DAILY = 'daily'; | ||
const CHANGEFREQ_WEEKLY = 'weekly'; | ||
const CHANGEFREQ_MONTHLY = 'monthly'; | ||
const CHANGEFREQ_YEARLY = 'yearly'; | ||
const CHANGEFREQ_NEVER = 'never'; | ||
|
||
/** @var \Closure $dataClosure */ | ||
public $dataClosure; | ||
|
||
/** @var string $defaultChangefreq */ | ||
public $defaultChangefreq = self::CHANGEFREQ_MONTHLY; | ||
|
||
/** @var float $defaultPriority */ | ||
public $defaultPriority = 0.5; | ||
|
||
public function init() | ||
{ | ||
if (!$this->dataClosure instanceof \Closure) { | ||
throw new InvalidConfigException('SitemapBehavior::$dataClosure isn`t \Closure object.'); | ||
} | ||
} | ||
|
||
public function generateSiteMap() | ||
{ | ||
$result = []; | ||
$n = 0; | ||
|
||
/** @var ActiveRecord $owner */ | ||
$owner = $this->owner; | ||
$models = $owner::find()->all(); | ||
|
||
foreach ($models as $model) { | ||
$urlData = call_user_func($this->dataClosure, $model); | ||
|
||
if (empty($urlData)) { | ||
continue; | ||
} | ||
|
||
if (!isset($urlData['loc'])) { | ||
throw new InvalidConfigException('Params `loc` isn`t set.'); | ||
} | ||
|
||
$result[$n]['loc'] = $urlData['loc']; | ||
if(isset($urlData['lastmod'])) { | ||
$result[$n]['lastmod'] = date(DATE_W3C, $urlData['lastmod']); | ||
} | ||
|
||
$result[$n]['changefreq'] = | ||
isset($urlData['changefreq']) ? $urlData['changefreq'] : $this->defaultChangefreq; | ||
$result[$n]['priority'] = | ||
isset($urlData['priority']) ? $urlData['priority'] : $this->defaultPriority; | ||
|
||
++$n; | ||
} | ||
return $result; | ||
} | ||
} | ||
<?php | ||
/** | ||
* @link https://github.com/himiklab/yii2-sitemap-module | ||
* @copyright Copyright (c) 2014 HimikLab | ||
* @license http://opensource.org/licenses/MIT | ||
*/ | ||
|
||
namespace himiklab\sitemap\behaviors; | ||
|
||
use yii\base\Behavior; | ||
use yii\db\ActiveRecord; | ||
use yii\base\InvalidParamException; | ||
|
||
/** | ||
* Behavior for XML Sitemap Yii2 module. | ||
* | ||
* For example: | ||
* | ||
* ```php | ||
* public function behaviors() | ||
* { | ||
* return [ | ||
* 'sitemap' => [ | ||
* 'class' => SitemapBehavior::className(), | ||
* 'dataClosure' => function ($model) { | ||
* return [ | ||
* 'loc' => Url::to(model->url, true), | ||
* 'lastmod' => strtotime($model->lastmod), | ||
* 'changefreq' => SitemapBehavior::CHANGEFREQ_DAILY, | ||
* 'priority' => 0.8 | ||
* ]; | ||
* } | ||
* ], | ||
* ]; | ||
* } | ||
* ``` | ||
* | ||
* @see http://www.sitemaps.org/protocol.html | ||
* @author HimikLab | ||
* @package himiklab\sitemap | ||
*/ | ||
class SitemapBehavior extends Behavior | ||
{ | ||
const CHANGEFREQ_ALWAYS = 'always'; | ||
const CHANGEFREQ_HOURLY = 'hourly'; | ||
const CHANGEFREQ_DAILY = 'daily'; | ||
const CHANGEFREQ_WEEKLY = 'weekly'; | ||
const CHANGEFREQ_MONTHLY = 'monthly'; | ||
const CHANGEFREQ_YEARLY = 'yearly'; | ||
const CHANGEFREQ_NEVER = 'never'; | ||
|
||
/** @var \Closure $dataClosure */ | ||
public $dataClosure; | ||
|
||
/** @var string|bool $defaultChangefreq */ | ||
public $defaultChangefreq = false; | ||
|
||
/** @var float|bool $defaultPriority */ | ||
public $defaultPriority = false; | ||
|
||
public function init() | ||
{ | ||
if (!$this->dataClosure instanceof \Closure) { | ||
throw new InvalidParamException('SitemapBehavior::$dataClosure isn`t \Closure object.'); | ||
} | ||
} | ||
|
||
public function generateSiteMap() | ||
{ | ||
$result = []; | ||
$n = 0; | ||
|
||
/** @var ActiveRecord $owner */ | ||
$owner = $this->owner; | ||
$models = $owner::find()->all(); | ||
|
||
foreach ($models as $model) { | ||
$urlData = call_user_func($this->dataClosure, $model); | ||
|
||
if (empty($urlData)) { | ||
continue; | ||
} | ||
|
||
if (!isset($urlData['loc'])) { | ||
throw new InvalidParamException('Required param `loc` isn`t set.'); | ||
} | ||
|
||
$result[$n]['loc'] = $urlData['loc']; | ||
if (isset($urlData['lastmod'])) { | ||
$result[$n]['lastmod'] = date(DATE_W3C, $urlData['lastmod']); | ||
} | ||
|
||
if (isset($urlData['changefreq'])) { | ||
$result[$n]['changefreq'] = $urlData['changefreq']; | ||
} elseif ($this->defaultChangefreq !== false) { | ||
$result[$n]['changefreq'] = $this->defaultChangefreq; | ||
} | ||
|
||
if (isset($urlData['priority'])) { | ||
$result[$n]['priority'] = $urlData['priority']; | ||
} elseif ($this->defaultPriority !== false) { | ||
$result[$n]['priority'] = $this->defaultPriority; | ||
} | ||
|
||
++$n; | ||
} | ||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,27 @@ | ||
<?php | ||
/** | ||
* @link https://github.com/himiklab/yii2-sitemap-module | ||
* @copyright Copyright (c) 2014 HimikLab | ||
* @license http://opensource.org/licenses/MIT | ||
* | ||
* @var array $urls | ||
*/ | ||
|
||
echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL; | ||
?> | ||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
<?php foreach ($urls as $url): ?> | ||
<url> | ||
<loc><?= $url['loc'] ?></loc> | ||
<?php if(isset($url['lastmod'])): ?> | ||
<lastmod><?= $url['lastmod'] ?></lastmod> | ||
<?php endif; ?> | ||
<changefreq><?= $url['changefreq'] ?></changefreq> | ||
<priority><?= $url['priority'] ?></priority> | ||
</url> | ||
<?php endforeach; ?> | ||
</urlset> | ||
<?php | ||
/** | ||
* @link https://github.com/himiklab/yii2-sitemap-module | ||
* @copyright Copyright (c) 2014 HimikLab | ||
* @license http://opensource.org/licenses/MIT | ||
* | ||
* @var array $urls | ||
*/ | ||
|
||
echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL; | ||
?> | ||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
<?php foreach ($urls as $url): ?> | ||
<url> | ||
<loc><?= $url['loc'] ?></loc> | ||
<?php if (isset($url['lastmod'])): ?> | ||
<lastmod><?= $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 endforeach; ?> | ||
</urlset> |