the_elephpant / laravel-xml-sitemap
PHP框架的简单站点地图生成器。
Requires
- php: ^7.1.3 || ^8.0
- ext-dom: *
- dragon-code/xml-core: ^2.1
- illuminate/database: ^5.6 || ^6.20.26 || ^7.30.3 || ^8.22.1 || ^9.0 || ^10.0
- illuminate/support: ^5.6 || ^6.20.26 || ^7.30.3 || ^8.22.1 || ^9.0 || ^10.0
- nesbot/carbon: ^1.25 || ^2.0
- symfony/http-foundation: ^4.0 || ^5.0 || ^6.0
Requires (Dev)
- phpunit/phpunit: ^9.0
Suggests
- symfony/thanks: Give thanks (in the form of a GitHub) to your fellow PHP package maintainers
This package is not auto-updated.
Last update: 2024-09-28 16:46:38 UTC
README
PHP框架的简单站点地图生成器。
安装
对于Laravel框架5.4和5.5版本,使用版本3。
要获取Laravel Sitemap的最新版本,只需使用Composer引入项目
composer require andrey-helldar/sitemap
当然,您也可以手动更新require块并运行composer update
,如果愿意的话
{ "require": { "andrey-helldar/sitemap": "^5.0" } }
如果您不使用自动发现,请将ServiceProvider
添加到config/app.php
中的providers数组中
Helldar\Sitemap\ServiceProvider::class,
您还可以发布配置文件以更改实现(例如,将接口更改为特定类)
php artisan vendor:publish --provider="Helldar\Sitemap\ServiceProvider"
现在您可以使用app('sitemap')
方法。
升级指南
配置
要配置生成,您需要在config/sitemap.php
文件中填写models
数组
'models' => [ \App\User::class => [ 'route' => 'route.name', 'route_parameters' => [ 'slug' => 'table_field_for_slug', 'foo' => 'table_field_for_foo', bar' => 'my_relation.slug', 'baz', ], 'lastmod' => 'updated_at', 'frequency' => 'daily', 'priority' => 0.8, ], ]
数组的键必须是将为它定义规则的模型名称。
- route - 生成URL的路由名称。
- route_parameters - 要传递给URL生成方法的参数,其中
- 键是路由参数的名称。如果路由参数的名称与数据库中的列名匹配,则可以仅指定值。
- 值是用于替换值的数据库列名。如果需要从链接中获取值,则应通过点指定调用。例如:
'bar' => 'my_relation.slug'
。
- lastmod - 是包含记录日期的列的名称。如果没有,则使用当前日期。如果模型不需要考虑时间字段,请将
lastmod
参数设置为false
。 - frequency - 是内容的刷新速率的值。这对于某些搜索引擎机器人是必要的。您也可以使用
Helldar\Sitemap\Services\Sitemap
类中的常量。 - priority - 是模型记录的引用优先级。
如果任何模型值未定义,则使用全局值。
使用
手动
您还可以传递手动创建的项目数组
use Carbon\Carbon; use Helldar\Sitemap\Helpers\Variables; use Helldar\Sitemap\Services\Sitemap; $items_a = []; $items_b = []; for ($i = 0; $i < 3; $i++) { $item = app('sitemap')->makeItem() ->changefreq('weekly') ->lastmod(Carbon::now()) ->loc("http://mysite.local/page/" . $i); array_push($items_a, $item); } for ($i = 0; $i < 3; $i++) { $item = app('sitemap')->makeItem() ->changefreq(Variables::FREQUENCY_WEEKLY) ->lastmod(Carbon::now()) ->loc("http://mysite.local/offers/" . $i); array_push($items_b, $item); } return app('sitemap') ->manual($items_a, $items_b) ->show();
返回
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <changefreq>weekly</changefreq> <lastmod>2018-03-06T12:30:17+03:00</lastmod> <loc>http://mysite.local/page/0</loc> <priority>0.5</priority> </url> <url> <changefreq>weekly</changefreq> <lastmod>2018-03-06T12:38:24+03:00</lastmod> <loc>http://mysite.local/page/1</loc> <priority>0.5</priority> </url> <url> <changefreq>weekly</changefreq> <lastmod>2018-03-06T12:30:17+03:00</lastmod> <loc>http://mysite.local/page/2</loc> <priority>0.5</priority> </url> <url> <changefreq>weekly</changefreq> <lastmod>2018-03-06T12:30:17+03:00</lastmod> <loc>http://mysite.local/offers/0</loc> <priority>0.5</priority> </url> <url> <changefreq>weekly</changefreq> <lastmod>2018-03-06T12:38:24+03:00</lastmod> <loc>http://mysite.local/offers/1</loc> <priority>0.5</priority> </url> <url> <changefreq>weekly</changefreq> <lastmod>2018-03-06T12:30:17+03:00</lastmod> <loc>http://mysite.local/offers/2</loc> <priority>0.5</priority> </url> </urlset>
您还可以将模型构建器中的数据与手动传递的数据结合使用
$query1 = \App\Catalog::query()->where('id', '>', '1000'); $query2 = \App\News::query()->where('category_id', 10); $query3 = \App\Pages::query(); return app('sitemap') ->builders($query1, $query2, $query3) ->manual($items1, $items2, $items3) ->show();
图像
您还可以传递手动创建的图像项目数组
use Helldar\Sitemap\Services\Sitemap; $items = []; for ($i = 0; $i < 2; $i++) { $item = app('sitemap')->makeImages() ->loc("http://mysite.local/page/" . $i) ->image("http://mysite.local/images/1.jpg", "My Title 1-".$i, "Caption for image", "Limerick, Ireland", "Royalty free") ->image("http://mysite.local/images/2.jpg", "My Title 2-".$i) ->image("http://mysite.local/images/3.jpg"); array_push($items, $item); } return app('sitemap') ->images($items) ->show();
返回
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"> <url> <loc>http://mysite.local/page/0</loc> <image:image> <image:loc>http://mysite.local/images/1.jpg</image:loc> <image:title>My Title 1-0</image:title> <image:caption>Caption for image</image:caption> <image:geo_location>Limerick, Ireland</image:geo_location> <image:license>Royalty free</image:license> </image:image> <image:image> <image:loc>http://mysite.local/images/2.jpg</image:loc> <image:title>My Title 2-0</image:title> </image:image> <image:image> <image:loc>http://mysite.local/images/3.jpg</image:loc> </image:image> </url> <url> <loc>http://mysite.local/page/1</loc> <image:image> <image:loc>http://mysite.local/images/1.jpg</image:loc> <image:title>My Title 1-1</image:title> <image:caption>Caption for image</image:caption> <image:geo_location>Limerick, Ireland</image:geo_location> <image:license>Royalty free</image:license> </image:image> <image:image> <image:loc>http://mysite.local/images/2.jpg</image:loc> <image:title>My Title 2-1</image:title> </image:image> <image:image> <image:loc>http://mysite.local/images/3.jpg</image:loc> </image:image> </url> </urlset>
注意!由于文档结构不同,尝试调用show()
方法时,如果没有任何其他方法调用,则只会显示图像地图。
示例
// Will show the image map. return app('sitemap') ->images($items) ->show(); // Shows the map for `builders`. The image map will be ignored. return app('sitemap') ->builders($query1, $query2, $query3) ->images($items) ->show();
当保存到一个文件时,也适用同样的原则 - 图像将被忽略。但是当保存到多个文件时,地图将成功创建。
显示
要显示屏幕上的内容,请使用show()
方法
$query1 = \App\Catalog::query()->where('id', '>', '1000'); $query2 = \App\News::query()->where('category_id', 10); $query3 = \App\Pages::query(); return app('sitemap') ->builders($query1, $query2, $query3) ->show();
要返回页面内容,请添加任何路由
app('route')->get('sitemap', function() { $query1 = \App\Catalog::query()->where('id', '>', '1000'); $query2 = \App\News::query()->where('category_id', 10); $query3 = \App\Pages::query(); return app('sitemap') ->builders($query1, $query2, $query3) ->show(); });
并转到您的URL。例如:http://mysite.dev/sitemap
。
保存
如果separate_files
选项被禁用
要保存内容到文件,请使用save()
方法
$query1 = \App\Catalog::query()->where('id', '>', '1000'); $query2 = \App\News::query()->where('category_id', 10); $query3 = \App\Pages::query(); app('sitemap') ->builders($query1, $query2, $query3) ->save();
如果您想保存多个文件,请将文件路径作为参数传递给save()
方法
$query1 = \App\Catalog::query()->where('id', '>', '1000'); $query2 = \App\News::query()->where('category_id', 10); $query3 = \App\Pages::query(); app('sitemap') ->builders($query1, $query2, $query3) ->save('sitemap-1.xml'); app('sitemap') ->builders($query1, $query2, $query3) ->save('foo/bar/sitemap-2.xml');
如果separate_files
选项被启用
要将内容保存到单独的文件中,请在 config/sitemap.php 文件中使用 save()
方法,并设置参数 'separate_files' => true
。
$query1 = \App\Catalog::query()->where('id', '>', '1000'); $query2 = \App\News::query()->where('category_id', 10); $query3 = \App\Pages::query(); app('sitemap') ->builders($query1, $query2, $query3) ->save();
在这种情况下,文件的名称将是设置中的默认名称:'filename' => 'sitemap.xml'
。这些文件将通过 Storage
门面保存(见 config/sitemap.php)。
每个模型构建器将被处理并保存在一个单独的文件中,共享文件将包含对其的引用(使用选定的 public
存储名称)。
/storage/sitemap.xml // general file
/storage/sitemap-1.xml // generated file for the $query1 collection
/storage/sitemap-2.xml // generated file for the $query2 collection
/storage/sitemap-3.xml // generated file for the $query3 collection
<?xml version="1.0" encoding="utf-8"?> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemap> <lastmod>2018-07-05T13:51:40+00:00</lastmod> <loc>http://example.com/storage/sitemap-1.xml</loc> </sitemap> <sitemap> <lastmod>2018-07-05T13:51:41+00:00</lastmod> <loc>http://example.com/storage/sitemap-2.xml</loc> </sitemap> <sitemap> <lastmod>2018-07-05T13:51:41+00:00</lastmod> <loc>http://example.com/storage/sitemap-3.xml</loc> </sitemap> </sitemapindex>
如果您使用的是多域名应用程序,您可以在设置中预先指定生成的其他文件的域名链接。
'domains' => [ 'foo' => env('APP_URL'), // http://example.com 'bar' => 'http://foo.bar', ],
app('sitemap') ->builders($query1, $query2, $query3) ->domain('foo') ->save(); app('sitemap') ->builders($query1, $query2, $query3) ->domain('bar') ->save();
此方法将创建具有以下链接的文件:
<?xml version="1.0" encoding="utf-8"?> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemap> <lastmod>2018-07-05T13:51:40+00:00</lastmod> <loc>http://example.com/storage/sitemap-1.xml</loc> </sitemap> <sitemap> <lastmod>2018-07-05T13:51:41+00:00</lastmod> <loc>http://example.com/storage/sitemap-2.xml</loc> </sitemap> <sitemap> <lastmod>2018-07-05T13:51:41+00:00</lastmod> <loc>http://example.com/storage/sitemap-3.xml</loc> </sitemap> </sitemapindex>
和
<?xml version="1.0" encoding="utf-8"?> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemap> <lastmod>2018-07-05T13:51:40+00:00</lastmod> <loc>http://foo.bar/storage/sitemap-1.xml</loc> </sitemap> <sitemap> <lastmod>2018-07-05T13:51:41+00:00</lastmod> <loc>http://foo.bar/storage/sitemap-2.xml</loc> </sitemap> <sitemap> <lastmod>2018-07-05T13:51:41+00:00</lastmod> <loc>http://foo.bar/storage/sitemap-3.xml</loc> </sitemap> </sitemapindex>
默认情况下,将使用 .env
文件中 APP_URL
指定的域名。
如果您想保存多个文件,请将文件路径作为参数传递给 save($path)
方法,并在 config/sitemap.php 文件中设置参数 'separate_files' => true
。
$query1 = \App\Catalog::query()->where('id', '>', '1000'); $query2 = \App\News::query()->where('category_id', 10); $query3 = \App\Pages::query(); $manual_items = []; for ($i = 0; $i < 3; $i++) { $item = app('sitemap')->makeItem() ->changefreq('weekly') ->lastmod(Carbon\Carbon::now()) ->loc("http://mysite.local/page/" . $i); array_push($manual_items, $item); } app('sitemap') ->builders($query1, $query2, $query3) ->manual($manual_items) ->save(public_path('first.xml')); app('sitemap') ->builders($query1, $query2, $query3) ->manual($manual_items) ->save(storage_path('foo/bar/second.xml'));
将创建文件
/storage/first.xml // general file
/storage/first-1.xml // generated file for the $query1 collection
/storage/first-2.xml // generated file for the $query2 collection
/storage/first-3.xml // generated file for the $query3 collection
/storage/first-4.xml // generated file for the $manual_items collection
/storage/foo/bar/second.xml // general file
/storage/foo/bar/second-1.xml // generated file for the $query1 collection
/storage/foo/bar/second-2.xml // generated file for the $query2 collection
/storage/foo/bar/second-3.xml // generated file for the $query3 collection
/storage/foo/bar/second-4.xml // generated file for the $manual_items collection
SEO
当搜索引擎机器人索引网站时,Sitemap 非常有用。如果您使用将 Sitemap 分割成多个文件的,您只需要在 robots.txt
文件中添加主文件的链接。
例如,您创建了几个文件
/storage/sitemaps/promo.xml
/storage/sitemaps/promo-1.xml
/storage/sitemaps/promo-2.xml
/storage/sitemaps/promo-3.xml
在 robots.txt
文件中,您只需要指定主文件的链接
Sitemap: http://example.com/storage/sitemaps/promo.xml
所有其他搜索引擎机器人都会为您完成。
祝您享受!
许可协议
本软件包遵循 MIT 许可协议。