mfonte / laravel-sitemap
Laravel 网站地图生成器。支持 SitemapIndex、图片网站地图和新闻网站地图。
Requires
- ext-openssl: >1.0.2
- illuminate/contracts: ^9.0 || ^10.0
- illuminate/support: ^9.0 || ^10.0
- nesbot/carbon: ^2.0
- spatie/laravel-package-tools: ^1.14
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.4
- mockery/mockery: ^1.3.3
- orchestra/testbench: ^7.0
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^9.5
- spatie/phpunit-snapshot-assertions: ^4.2
- spatie/temporary-directory: ^2.1
This package is auto-updated.
Last update: 2024-09-30 14:02:52 UTC
README
此包是从 spatie/laravel-sitemap 分支出来的,以移除对 SitemapGenerator 的支持,移除 PHP 8 的安装要求,并添加对 图片网站地图 和 新闻网站地图 的支持。
此外,此包还支持在 非 Laravel 项目 外部安装。请参阅 在 Laravel 外部使用此包 部分。
此包可以通过此包提供的 API 编写自定义逻辑来生成有效的网站地图结构。
注意!此包需要 Laravel 9 或 Laravel 10。对于 PHP 7.4 和 Laravel 8 兼容性,请参阅 v1.1.*。
安装
对于 Laravel 9 或 10,或对于运行在 PHP >= 8.0 的非 Laravel 项目
composer require mfonte/laravel-sitemap
对于 Laravel 8,或对于运行在 PHP >= 7.4 && < 8.0 的非 Laravel 项目
composer require mfonte/laravel-sitemap "^1.1"
创建网站地图
您只能手动创建网站地图
use Carbon\Carbon; use Mfonte\Sitemap\Sitemap; use Mfonte\Sitemap\Tags\Url; Sitemap::create() ->add( Url::create('/home') ->setLastModificationDate(Carbon::yesterday()) ->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY) ->setPriority(0.1) ->addImage('/path/to/image', 'A wonderful Caption') ->addNews('A long story short', 'en', Carbon::yesterday(), 'Sitemaps are this great!') ) ->add(...) ->writeToFile($path);
网站地图生成器可以自动理解您在网站地图中放置的项目类型,并据此创建有效的模式。以下是一个包含图片和新闻的网站地图头部的示例
<?xml version="1.0" encoding="UTF-8"?>\n <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"> <url> <loc>https:///page10</loc>\n <lastmod>2016-01-01T00:00:00+00:00</lastmod> <changefreq>daily</changefreq> <priority>0.8</priority> <image:image> <image:loc>https:///imageUrl</image:loc> </image:image> <news:news> <news:publication_date>2015-12-29</news:publication_date> <news:title>defaultTitle</news:title> <news:publication> <news:name>defaultName</news:name> <news:language>defaultLanguage</news:language> </news:publication> </news:news> </url> </urlset>
您也可以通过实现 \Mfonte\Sitemap\Contracts\Sitemapable 接口直接添加您的模型。
use Mfonte\Sitemap\Contracts\Sitemapable; use Mfonte\Sitemap\Tags\Url; class Post extends Model implements Sitemapable { public function toSitemapTag(): Url | string | array { return route('blog.post.show', $this); } }
现在您可以将单个帖子模型添加到网站地图,甚至整个集合。
use Mfonte\Sitemap\Sitemap; Sitemap::create() ->add($post) ->add(Post::all());
这样,您可以在不爬取所有页面的情况下快速添加所有页面。
创建网站地图索引
您可以创建网站地图索引
use Mfonte\Sitemap\SitemapIndex; SitemapIndex::create() ->add('/pages_sitemap.xml') ->add('/posts_sitemap.xml') ->writeToFile($sitemapIndexPath);
您可以传递一个 Mfonte\Sitemap\Tags\Sitemap 对象来手动设置 lastModificationDate 属性。
use Mfonte\Sitemap\SitemapIndex; use Mfonte\Sitemap\Tags\Sitemap; SitemapIndex::create() ->add('/pages_sitemap.xml') ->add(Sitemap::create('/posts_sitemap.xml') ->setLastModificationDate(Carbon::yesterday())) ->writeToFile($sitemapIndexPath);
生成的网站地图索引将类似于以下内容
<?xml version="1.0" encoding="UTF-8"?> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemap> <loc>http://www.example.com/pages_sitemap.xml</loc> <lastmod>2016-01-01T00:00:00+00:00</lastmod> </sitemap> <sitemap> <loc>http://www.example.com/posts_sitemap.xml</loc> <lastmod>2015-12-31T00:00:00+00:00</lastmod> </sitemap> </sitemapindex>
Laravel 外部使用
上述说明同样适用,除了
- 您 不能 使用
Contracts\Sitemapable来扩展 模型(您不在 Laravel 中,对吧?) - 您 必须 通过提供额外的布尔标志
$nativeRenderer = true使用Sitemap::render()、Sitemap::writeToFile()、SitemapIndex::render()和SitemapIndex::writeToFile() - 您 不能 使用
Sitemap::writeToDisk()、Sitemap::toResponse()、SitemapIndex::writeToDisk()和SitemapIndex::toResponse()
因此,例如,基本方法可能如下
use Carbon\Carbon; use Mfonte\Sitemap\Sitemap; use Mfonte\Sitemap\Tags\Url; $sitemapStream = Sitemap::create()->add( Url::create('/home') ->setLastModificationDate(Carbon::yesterday()) ->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY) ->setPriority(0.1) ->addImage('/path/to/image', 'A wonderful Caption') ->addNews('A long story short', 'en', Carbon::yesterday(), 'Sitemaps are this great!') ) ->add(...) ->render(nativeRenderer: true);
变更日志
请参阅 变更日志 获取更多最近更改的信息。
测试
$ composer test
致谢
许可
MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件。 此包已从 https://github.com/spatie/laravel-sitemap 分支出来,相关的许可文件已迁移到本存储库中。