voskobovich/yii2-sitemap-module

自动生成XML Sitemap的Yii2模块

安装: 32

依赖项: 0

建议者: 0

安全: 0

星标: 2

关注者: 2

分支: 0

开放问题: 0

类型:yii2-extension

dev-master 2015-03-24 13:43 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:05:09 UTC


README

为自动生成XML Sitemap的Yii2模块。

安装

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

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

或者将以下内容添加到您的应用配置文件中的require部分:

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

to the require section of your application's composer.json file.

  • 配置您应用的配置文件中的cache组件,例如
'components' => [
    'cache' => [
        'class' => 'yii\caching\FileCache',
    ],
]
  • 在您应用的配置文件中的modules部分添加一个新的模块,例如
'modules' => [
    'sitemap' => [
        'class' => 'voskobovich\sitemap\Module',
        'models' => [
            // your models
            'app\modules\news\models\News',
            // or configuration for creating a behavior
            [
                'class' => 'app\modules\news\models\News',
                'config' => [
                    '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
                        ];
                    }
                ],
            ],
        ],
        'urls'=> [
            // your additional urls
            [
                'loc' => '/news/index',
                'changefreq' => \voskobovich\sitemap\behaviors\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 voskobovich\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'],
    ],
],

资源