migvitram / yii2-xml-generator
动态生成XML文件(例如 sitemap.xml)的模块
v1.0.0
2020-04-25 18:10 UTC
Requires
- yiisoft/yii2: ^2.0.13
This package is auto-updated.
Last update: 2024-09-20 16:52:26 UTC
README
动态生成XML文件(例如 sitemap.xml)的模块
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一
php composer.phar require --prefer-dist migvitram/yii2-xml-generator "*"
或者
"migvitram/yii2-xml-generator": "*"
将以下内容添加到您的 composer.json
文件的 require-dev 部分中。
使用方法
安装后,修改 web.php 配置文件中的 modules 部分
'modules' => [ ... 'xmlGenerator' => [ 'class' => 'migvitram\xmlgenerator\XmlGeneratorModule', 'pages' => ['\app\models\Page', 'getPagesForSitemap'], 'atom' => ['\app\models\Page', 'getItemsForAtom'], 'rss' => ['\app\models\Page', 'getItemsForRss'], ] ]
,在这里可以通过回调函数传递数组,用于生成 sitemap.xml、atom.xml 和 rss.xml 文件的项目项。
回调方法必须生成数组,其中每个子元素都需要满足以下格式
[ ... [ 'loc' => Url::base(true) . 'your/pages/url', 'lastmod' => '2016-10-10', // date of page modification in format 'Y-m-d' 'changefreq' => migvitram\xmlgenerator\models\SitemapSchema::CHANG_FREQ_MONTH, // frequency of changing 'priority' => 0.8, // priority in double type number ], ... ]
,根据 sitemap 协议。
例如,Page 模型中的 getPagesForSitemap
方法可能如下所示
namespace your\app\namespace; use yii\base\Model; use yii\helpers\Url; use migvitram\xmlgenerator\models\schemas\Sitemap\SitemapSchema; class Page extends Model { /** * @return array */ public static function getPagesForSitemap() { return [ [ 'loc' => Url::base(true), 'lastmod' => '2016-10-10', 'changefreq' => SitemapSchema::CHANG_FREQ_DAY, 'priority' => 0.8, ], [ 'loc' => Url::base(true).'/site/about', 'lastmod' => '2016-10-10', 'changefreq' => SitemapSchema::CHANG_FREQ_YEAR, 'priority' => 0.8, ], [ 'loc' => Url::base(true).'/site/contact', 'lastmod' => '2016-10-10', 'changefreq' => SitemapSchema::CHANG_FREQ_MONTH, 'priority' => 0.8, ], [ 'loc' => Url::base(true).'/site/news', 'lastmod' => '2016-10-10', 'changefreq' => 'monthly', 'priority' => 0.8, ], ]; } }
获取 rss.xml 数据的方法必须返回包含以下所需字段的数组
use migvitram\xmlgenerator\models\schemas\Rss\RssSchema; class Page extends Model { /** * @return array */ public static function getItemsForRss() { // gather all needed news return [ RssSchema::TITLE_FIELD => 'some title', // RssSchema constant for field name can be used RssSchema::LINK_FIELD => 'soem link /', RssSchema::DESCRIPTION_FIELD => 'description here', 'image' => [ 'title' => 'aodfijoisdjf IMAGE', 'link' => Url::base(true).'/aodfijoisdjf987', 'url' => 'aodifj/asodifj.sod' ], 'language' => 'ru', 'items' => [ [ RssSchema::TITLE_FIELD => '1 title of entry', RssSchema::LINK_FIELD => 'http://example.org/2003/12/13/atom03', RssSchema::DESCRIPTION_FIELD => 'asdf joiasdjf oiajsdfa9s8dhf ajksdnf admfa suidhf9 ashd9f8h', 'someOption' => 'asdfoij' ], [ RssSchema::TITLE_FIELD => '2 title of entry', RssSchema::LINK_FIELD => 'http://example.org/2003/12/13/atom03', RssSchema::DESCRIPTION_FIELD => 'asdf joiasdjf oiajsdfa9s8dhf ajksdnf admfa suidhf9 ashd9f8h', 'author' => 'ADFd Adfid', ], [ RssSchema::TITLE_FIELD => '3 title of entry', RssSchema::LINK_FIELD => 'http://example.org/2003/12/13/atom03', RssSchema::DESCRIPTION_FIELD => 'asdf joiasdjf oiajsdfa9s8dhf ajksdnf admfa suidhf9 ashd9f8h', 'comments' => 'aodsfijoasidfjoij/aoidjsfoijadf/aoidsjf', ], ], ]; } }
items 和 channel 部分可以包含可选字段,具体取决于 rss 文档
获取 atom.xml 数据的方法必须返回包含 Atom feed 所需字段和所需 items
数组的数组
use migvitram\xmlgenerator\models\schemas\Atom\AtomSchema; class Page extends Model { /** * @return array */ public static function getItemsForAtom() { // gather all needed news return [ 'title' => 'Feed title', 'link' => 'your/site/url', 'updated' => '2020-04-10T11:50Z', 'author' => 'John Doe', 'id' => 'urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6 or some your own', 'items' => [ [ AtomSchema::ENTRY_TITLE_FIELD => '1 title of entry', // AtomSchema constant for field name can be used AtomSchema::ENTRY_LINK_FIELD => 'http://example.org/2003/12/13/atom01', AtomSchema::ENTRY_UPDATE_FIELD => '2016-10-10T6:50Z', AtomSchema::ENTRY_SUMMARY_FIELD => 'Some summary about article', AtomSchema::ENTRY_ID_FIELD => 'urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6 or some your own', ], [ 'title' => '2 title of entry', 'link' => 'http://example.org/2003/12/13/atom02', 'updated' => '2016-10-10T6:50Z', 'summary' => 'Some summary about article', 'id' => 'urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6 or some your own', ], [ 'title' => '3 title of entry', 'link' => 'http://example.org/2003/12/13/atom03', 'updated' => '2016-10-10T6:50Z', 'summary' => 'Some summary about article', 'id' => 'urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6 or some your own', ], ], ]; } }
,根据 atom.xml 文档。