himiklab/yii2-sitemap-module

此软件包已被废弃,不再维护。未建议替代软件包。

自动生成XML Sitemap的Yii2模块

安装量: 162,826

依赖项: 6

建议者: 0

安全性: 0

星级: 91

关注者: 11

分支: 39

公开问题: 3

类型:yii2-extension

1.2.1 2019-02-11 07:32 UTC

This package is auto-updated.

Last update: 2022-01-12 15:40:21 UTC


README

Yii2模块,用于自动生成XML Sitemap

Packagist Packagist license

安装

安装此扩展的首选方式是通过 composer

  • 运行以下命令之一
php composer.phar require --prefer-dist "himiklab/yii2-sitemap-module" "*"

"himiklab/yii2-sitemap-module" : "*"

将以下内容添加到您的应用程序的 composer.json 文件的 require 部分。

  • 配置应用程序配置文件中的 cache 组件,例如
'components' => [
    'cache' => [
        'class' => 'yii\caching\FileCache',
    ],
]
  • 在配置文件开始处添加 use himiklab\sitemap\behaviors\SitemapBehavior;

  • 在应用程序配置文件的 modules 部分中添加新的模块,例如

'modules' => [
    'sitemap' => [
        'class' => 'himiklab\sitemap\Sitemap',
        'models' => [
            // your models
            'app\modules\news\models\News',
            // or configuration for creating a behavior
                [
                    'class' => 'app\modules\news\models\News',
                    'behaviors' => [
                        'sitemap' => [
                            'class' => SitemapBehavior::className(),
                            'scope' => function ($model) {
                                /** @var \yii\db\ActiveQuery $model */
                                $model->select(['url', 'lastmod']);
                                $model->andWhere(['is_deleted' => 0]);
                            },
                            'dataClosure' => function ($model) {
                                /** @var self $model */
                                return [
                                    'loc' => Url::to($model->url, true),
                                    'lastmod' => strtotime($model->lastmod),
                                    'changefreq' => SitemapBehavior::CHANGEFREQ_DAILY,
                                    'priority' => 0.8,
                                    'xhtml:link' => [
                                        [
                                            'hreflang' => 'en',
                                            'href' => Url::to(['url_en'], true),
                                        ],
                                        [
                                            'hreflang' => 'de',
                                            'href' => Url::to(['url_de'], true),
                                        ],
                                        //...
                                    ],
                                ];
                            }
                        ],
                    ],
                ],
            ],
        ],
        'urls'=> [
            // your additional urls
            [
                'loc' => '/news/index',
                'changefreq' => SitemapBehavior::CHANGEFREQ_DAILY,
                'priority' => 0.8,
                'news' => [
                    'publication'   => [
                        'name'          => 'Example Blog',
                        'language'      => 'en',
                    ],
                    'access'            => 'Subscription',
                    'genres'            => 'Blog, UserGenerated',
                    'publication_date'  => 'YYYY-MM-DDThh:mm:ssTZD',
                    'title'             => 'Example Title',
                    'keywords'          => 'example, keywords, comma-separated',
                    'stock_tickers'     => 'NASDAQ:A, NASDAQ:B',
                ],
                'images' => [
                    [
                        'loc'           => 'http://example.com/image.jpg',
                        'caption'       => 'This is an example of a caption of an image',
                        'geo_location'  => 'City, State',
                        'title'         => 'Example image',
                        'license'       => 'http://example.com/license',
                    ],
                ],
            ],
        ],
        'enableGzip' => true, // default is false
        'cacheExpire' => 1, // 1 second. Default is 24 hours
    ],
],
  • 在AR模型中添加行为,例如
use himiklab\sitemap\behaviors\SitemapBehavior;

public function behaviors()
{
    return [
        'sitemap' => [
            'class' => SitemapBehavior::className(),
            'scope' => function ($model) {
                /** @var \yii\db\ActiveQuery $model */
                $model->select(['url', 'lastmod']);
                $model->andWhere(['is_deleted' => 0]);
            },
            'dataClosure' => function ($model) {
                /** @var self $model */
                return [
                    'loc' => Url::to($model->url, true),
                    'lastmod' => strtotime($model->lastmod),
                    'changefreq' => SitemapBehavior::CHANGEFREQ_DAILY,
                    'priority' => 0.8
                ];
            }
        ],
    ];
}
  • 为应用程序配置文件的 urlManager 添加新的规则,例如
'urlManager' => [
    'rules' => [
        ['pattern' => 'sitemap', 'route' => 'sitemap/default/index', 'suffix' => '.xml'],
    ],
],

资源