Laravel Sitemap 生成工具——laravelium/sitemap
安装
运行以下命令并提供最新的稳定版本(例如 v3.1.*
):
composer require laravelium/sitemap
或者将以下内容添加到您的 composer.json
文件中:
Laravel 5.8
"laravelium/sitemap": "3.1.*"
开发环境
"laravelium/sitemap": "3.1.x-dev"
Laravel 5.7
"laravelium/sitemap": "3.0.*"
开发环境
"laravelium/sitemap": "3.0.x-dev"
Laravel 5.6
"laravelium/sitemap": "2.8.*"
开发环境
"laravelium/sitemap": "2.8.x-dev"
Laravel 5.5
"laravelium/sitemap": "2.7.*"
开发环境
"laravelium/sitemap": "2.7.x-dev"
发布所需的资源(样式,视图,配置文件):
php artisan vendor:publish --provider="Laravelium\Sitemap\SitemapServiceProvider"
注意: 之后 composer update
,Composer
不会自动更新它们,您需要手动完成!
实例
如何生成动态站点地图(可选缓存)
Route::get('sitemap', function() {
// create new sitemap object
$sitemap = App::make('sitemap');
// set cache key (string), duration in minutes (Carbon|Datetime|int), turn on/off (boolean)
// by default cache is disabled
$sitemap->setCache('laravel.sitemap', 60);
// check if there is cached sitemap and build new only if is not
if (!$sitemap->isCached()) {
// add item to the sitemap (url, date, priority, freq)
$sitemap->add(URL::to('/'), '2012-08-25T20:10:00+02:00', '1.0', 'daily');
$sitemap->add(URL::to('page'), '2012-08-26T12:30:00+02:00', '0.9', 'monthly');
// add item with translations (url, date, priority, freq, images, title, translations)
$translations = [
['language' => 'fr', 'url' => URL::to('pageFr')],
['language' => 'de', 'url' => URL::to('pageDe')],
['language' => 'bg', 'url' => URL::to('pageBg')],
];
$sitemap->add(URL::to('pageEn'), '2015-06-24T14:30:00+02:00', '0.9', 'monthly', [], null, $translations);
// add item with images
$images = [
['url' => URL::to('images/pic1.jpg'), 'title' => 'Image title', 'caption' => 'Image caption', 'geo_location' => 'Plovdiv, Bulgaria'],
['url' => URL::to('images/pic2.jpg'), 'title' => 'Image title2', 'caption' => 'Image caption2'],
['url' => URL::to('images/pic3.jpg'), 'title' => 'Image title3'],
];
$sitemap->add(URL::to('post-with-images'), '2015-06-24T14:30:00+02:00', '0.9', 'monthly', $images);
// get all posts from db
$posts = DB::table('posts')->orderBy('created_at', 'desc')->get();
// add every post to the sitemap
foreach ($posts as $post) {
$sitemap->add($post->slug, $post->modified, $post->priority, $post->freq);
}
}
// show your sitemap (options: 'xml' (default), 'html', 'txt', 'ror-rss', 'ror-rdf')
return $sitemap->render('xml');
});
在下面这个例子中 posts
模型与 images
之间是 一对多
的关系
Route::get('sitemap', function() {
// create new sitemap object
$sitemap = App::make('sitemap');
// set cache key (string), duration in minutes (Carbon|Datetime|int), turn on/off (boolean)
// by default cache is disabled
$sitemap->setCache('laravel.sitemap', 60);
// check if there is cached sitemap and build new only if is not
if (!$sitemap->isCached()) {
// add item to the sitemap (url, date, priority, freq)
$sitemap->add(URL::to('/'), '2012-08-25T20:10:00+02:00', '1.0', 'daily');
$sitemap->add(URL::to('page'), '2012-08-26T12:30:00+02:00', '0.9', 'monthly');
// get all posts from db, with image relations
$posts = \DB::table('posts')->with('images')->orderBy('created_at', 'desc')->get();
// add every post to the sitemap
foreach ($posts as $post) {
// get all images for the current post
$images = array();
foreach ($post->images as $image) {
$images[] = array(
'url' => $image->url,
'title' => $image->title,
'caption' => $image->caption
);
}
$sitemap->add($post->slug, $post->modified, $post->priority, $post->freq, $images);
}
}
// show your sitemap (options: 'xml' (default), 'html', 'txt', 'ror-rss', 'ror-rdf')
return $sitemap->render('xml');
});
如何将多个站点地图用于站点地图索引
posts:
Route::get('sitemap-posts', function()
{
// create sitemap
$sitemap_posts = App::make("sitemap");
// set cache
$sitemap_posts->setCache('laravel.sitemap-posts', 3600);
// add items
$posts = DB::table('posts')->orderBy('created_at', 'desc')->get();
foreach ($posts as $post)
{
$sitemap_posts->add($post->slug, $post->modified, $post->priority, $post->freq);
}
// show sitemap
return $sitemap_posts->render('xml');
});
tags:
Route::get('sitemap-tags', function()
{
// create sitemap
$sitemap_tags = App::make("sitemap");
// set cache
$sitemap_tags->setCache('laravel.sitemap-tags', 3600);
// add items
$tags = DB::table('tags')->get();
foreach ($tags as $tag)
{
$sitemap_tags->add($tag->slug, null, '0.5', 'weekly');
}
// show sitemap
return $sitemap_tags->render('xml');
});
index:
Route::get('sitemap', function()
{
// create sitemap
$sitemap = App::make("sitemap");
// set cache
$sitemap->setCache('laravel.sitemap-index', 3600);
// add sitemaps (loc, lastmod (optional))
$sitemap->addSitemap(URL::to('sitemap-posts'));
$sitemap->addSitemap(URL::to('sitemap-tags'));
// show sitemap
return $sitemap->render('sitemapindex');
});
如何使用站点地图索引的多个站点地图(使用 store()
方法)
Route::get('sitemap-store', function()
{
// create sitemap
$sitemap_posts = App::make("sitemap");
// add items
$posts = DB::table('posts')->orderBy('created_at', 'desc')->get();
foreach ($posts as $post)
{
$sitemap_posts->add($post->slug, $post->modified, $post->priority, $post->freq);
}
// create file sitemap-posts.xml in your public folder (format, filename)
$sitemap_posts->store('xml','sitemap-posts');
// create sitemap
$sitemap_tags = App::make("sitemap");
// add items
$tags = DB::table('tags')->get();
foreach ($tags as $tag)
{
$sitemap_tags->add($tag->slug, null, '0.5', 'weekly');
}
// create file sitemap-tags.xml in your public folder (format, filename)
$sitemap_tags->store('xml','sitemap-tags');
// create sitemap index
$sitemap = App::make ("sitemap");
// add sitemaps (loc, lastmod (optional))
$sitemap->addSitemap(URL::to('sitemap-posts'));
$sitemap->addSitemap(URL::to('sitemap-tags'));
// create file sitemap.xml in your public folder (format, filename)
$sitemap->store('sitemapindex','sitemap');
});
如何生成站点地图文件
Route::get('mysitemap', function(){
// create new sitemap object
$sitemap = App::make("sitemap");
// add items to the sitemap (url, date, priority, freq)
$sitemap->add(URL::to(), '2012-08-25T20:10:00+02:00', '1.0', 'daily');
$sitemap->add(URL::to('page'), '2012-08-26T12:30:00+02:00', '0.9', 'monthly');
// get all posts from db
$posts = DB::table('posts')->orderBy('created_at', 'desc')->get();
// add every post to the sitemap
foreach ($posts as $post)
{
$sitemap->add($post->slug, $post->modified, $post->priority, $post->freq);
}
// generate your sitemap (format, filename)
$sitemap->store('xml', 'mysitemap');
// this will generate file mysitemap.xml to your public folder
});
如何生成大型站点地图(超过1000 000个元素)
Route::get('BigSitemap', function() {
// create new sitemap object
$sitemap = App::make('sitemap');
// get all products from db (or wherever you store them)
$products = DB::table('products')->orderBy('created_at', 'desc')->get();
// counters
$counter = 0;
$sitemapCounter = 0;
// add every product to multiple sitemaps with one sitemap index
foreach ($products as $p) {
if ($counter == 50000) {
// generate new sitemap file
$sitemap->store('xml', 'sitemap-' . $sitemapCounter);
// add the file to the sitemaps array
$sitemap->addSitemap(secure_url('sitemap-' . $sitemapCounter . '.xml'));
// reset items array (clear memory)
$sitemap->model->resetItems();
// reset the counter
$counter = 0;
// count generated sitemap
$sitemapCounter++;
}
// add product to items array
$sitemap->add($p->slug, $p->modified, $p->priority, $p->freq);
// count number of elements
$counter++;
}
// you need to check for unused items
if (!empty($sitemap->model->getItems())) {
// generate sitemap with last items
$sitemap->store('xml', 'sitemap-' . $sitemapCounter);
// add sitemap to sitemaps array
$sitemap->addSitemap(secure_url('sitemap-' . $sitemapCounter . '.xml'));
// reset items array
$sitemap->model->resetItems();
}
// generate new sitemapindex that will contain all generated sitemaps above
$sitemap->store('sitemapindex', 'sitemap');
});
如何在 Lumen
上生成站点地图文件
首先,通过运行 composer require illuminate/routing
命令,来包含 illuminate/routing
然后使用以下代码创建一个帮助类helper
,并将其添加到您的composer
自动加载中,或者只需将其添加到bootstrap/app.php
:
<?php
if (!function_exists('config_path'))
{
/**
* Get the configuration path.
*
* @param string $path
* @return string
*/
function config_path($path = '')
{
return app()->basePath() . '/config' . ($path ? '/' . $path : $path);
}
}
if (!function_exists('public_path'))
{
/**
* Get the public path.
*
* @param string $path
* @return string
*/
function public_path($path = '')
{
return app()->basePath() . '/public' . ($path ? '/' . $path : $path);
}
}
确保您已在bootstrap/app.php
中取消$app->withFacades()
的注释。并将以下代码添加到bootstrap/app.php
中的 Container Bindings
部分
$app->singleton(
Illuminate\Contracts\Routing\ResponseFactory::class,
Illuminate\Routing\ResponseFactory::class
);
同样在 bootstrap/app.php
中添加以下内容:$app->register(Roumen\Sitemap\SitemapServiceProvider::class);
在您的 config
文件夹中创建 sitemap.php
文件,文件内容:
<?php
/* Simple configuration file for Laravel Sitemap package */
return [
'use_cache' => false,
'cache_key' => 'laravel-sitemap.' . config('app.url'),
'cache_duration' => 3600,
'escaping' => true,
'use_limit_size' => false,
'max_size' => null,
'use_styles' => true,
'styles_location' => null,
];
最后,将以下内容添加到您的 routes/web.php
$app->get('mysitemap', function(){
// create new sitemap object
$sitemap = app()->make("sitemap");
// add items to the sitemap (url, date, priority, freq)
$sitemap->add(\URL::to(), '2012-08-25T20:10:00+02:00', '1.0', 'daily');
$sitemap->add(\URL::to('page'), '2012-08-26T12:30:00+02:00', '0.9', 'monthly');
// get all posts from db
$posts = \DB::table('posts')->orderBy('created_at', 'desc')->get();
// add every post to the sitemap
foreach ($posts as $post)
{
$sitemap->add($post->slug, $post->modified, $post->priority, $post->freq);
}
// generate your sitemap (format, filename)
$sitemap->store('xml', 'mysitemap');
// this will generate file mysitemap.xml to your public folder
});