veiliglanceren/laravel-seo-sitemap
Composer 安装命令:
composer require veiliglanceren/laravel-seo-sitemap
包简介
Laravel Sitemap package to optimize your website in search engines
README 文档
README
Laravel SEO Sitemap
Want better Google rankings? Generating a clean and up-to-date sitemap is one of the easiest wins for your website’s SEO. With this package, your sitemap is always synced with your route and content structure, no manual edits needed. Search engines like Google and Bing use your sitemap to crawl your site smarter and faster, which means your new pages and updates show up in search results sooner. Whether you're running a blog, webshop, or custom platform, an automated sitemap gives you an edge in visibility and indexing accuracy.
Lightweight. Extensible. Template-driven.
🚀 Features of SEO Laravel Sitemap
- 🔍 Automatic sitemap generation from named routes via
->sitemap() - 🧩 Advanced route templates via
->sitemapUsing(MyTemplate::class) - 🧠 Built-in
Templateabstract with helpers likeurlsFromModel() - ✏️ Configure
lastmod,priority,changefreqper URL - 💾 Save or serve sitemaps via disk or route
- 🧪 Fully tested with Pest and Laravel Testbench
- 📦 Optional meta-tag injection in
<head> - ✅ Laravel 10, 11, and 12 support
📦 Installation of the Laravel sitemap package
This package is quick to set up and works out-of-the-box with Laravel 10, 11, and 12. After installing via Composer, you can instantly publish the sitemap route and configuration using a single command. The php artisan sitemap:install command automatically adds a new sitemap.php route file and wires it into your existing web.php, so your sitemap is live without extra setup. It’s the easiest way to boost your SEO visibility with structured sitemap data.
composer require veiliglanceren/laravel-seo-sitemap
Publish the route & config:
php artisan sitemap:install php artisan vendor:publish --tag=sitemap-config
🧭 How to use the sitemap package
This package offers a clean and developer-friendly approach to sitemap generation in Laravel. Whether you're working with static pages or dynamic content from models, adding them to your sitemap is seamless. Use a single macro call for simple routes, or create powerful model-driven templates using the built-in abstract Template class to handle large, dynamic datasets. With just a few lines of code, your entire site structure becomes SEO-friendly and ready for search engine indexing.
✅ Static routes implemented in sitemap by 1 line in the routes/web.php file
The Route is getting implemented by calling the ->sitemap() Macro.
use VeiligLanceren\LaravelSeoSitemap\Support\Enums\ChangeFrequency; Route::get('/contact', ContactController::class) ->name('contact') ->sitemap() ->changefreq(ChangeFrequency::WEEKLY) ->priority('0.8');
Available Route Macros
The package includes expressive route macros that make it easy to configure sitemap settings directly in your routes/web.php file.
->sitemap()
Marks the route as sitemap-included.
Route::get('/about', AboutController::class) ->name('about') ->sitemap();
->changefreq(ChangeFrequency $frequency)
Defines how frequently the content at the URL is likely to change.
use VeiligLanceren\LaravelSeoSitemap\Support\Enums\ChangeFrequency; Route::get('/blog', BlogController::class) ->name('blog.index') ->sitemap() ->changefreq(ChangeFrequency::WEEKLY);
->priority(string $priority)
Sets the priority of this URL relative to other URLs on your site.
Route::get('/contact', ContactController::class) ->name('contact') ->sitemap() ->priority('0.8');
->lastmod(string|DateTimeInterface $date)
Sets the last modification date of the URL.
Route::get('/about', AboutController::class) ->name('about') ->sitemap() ->lastmod('2024-05-01');
💡 These macros can be chained for fluent configuration and better readability.
🧩 Model-driven Template class for easy implementation in sitemap
Use a custom Template that extends the abstract Template class:
// routes/web.php Route::get('/blog/{slug}', BlogController::class) ->name('blog.show') ->sitemapUsing(\App\Sitemap\Templates\PostTemplate::class);
Example custom Template for implementing dynamic routes in sitemap
Read more about all of the helper functions: template helper functions
namespace App\Sitemap\Templates; use App\Models\Post; use Illuminate\Routing\Route; use VeiligLanceren\LaravelSeoSitemap\Sitemap\Item\Url; use VeiligLanceren\LaravelSeoSitemap\Sitemap\Template; class PostTemplate extends Template { public function generate(Route $route): iterable { yield from $this->urlsFromModel(Post::class, $route, function (Post $post, Route $route) { return Url::make(route($route->getName(), ['slug' => $post->slug])) ->lastmod($post->updated_at) ->priority(0.6); }); } }
📂 Make an index for multiple sitemaps
Generate an index that references multiple sitemap files (e.g. per section):
use VeiligLanceren\LaravelSeoSitemap\Sitemap\SitemapIndex; $sitemapIndex = SitemapIndex::make('https://example.com/sitemap-pages.xml') ->add('https://example.com/sitemap-posts.xml');
You can dynamically add entries with an optional lastmod and pretty-print XML:
$sitemapIndex->add('https://example.com/sitemap-products.xml', now()); Storage::disk('public')->put('sitemap.xml', $sitemapIndex->toXml());
Alternatively, mark routes with an index and let the CLI generate the index and files for you:
Route::get('/blog', fn () => 'Blog') ->sitemapIndex('blog'); Route::get('/pages', fn () => 'Pages') ->sitemapIndex('pages'); // php artisan sitemap:generate
This will produce sitemap-blog.xml, sitemap-pages.xml and an sitemap.xml index linking to them.
📖 Read more: docs/sitemapindex.md
🧪 Generating sitemaps
use VeiligLanceren\LaravelSeoSitemap\Facades\Sitemap; Sitemap::fromRoutes() ->getSitemap() ->save('sitemap.xml', 'public');
Or use the CLI:
php artisan sitemap:generate
🖼 Add images to the sitemap
Using Url instances directly
use VeiligLanceren\LaravelSeoSitemap\Sitemap\Item\Url; use VeiligLanceren\LaravelSeoSitemap\Sitemap\Item\Image; $url = Url::make('https://example.com') ->addImage(Image::make('https://example.com/image1.jpg')->title('Hero 1')) ->addImage(Image::make('https://example.com/image2.jpg')->title('Hero 2'));
Via route macros
Route::get('/about', AboutController::class) ->name('about') ->image('https://example.com/hero.jpg');
🔗 Meta tag helper
<head> {!! Sitemap::meta() !!} </head>
Outputs:
<link rel="sitemap" type="application/xml" title="Sitemap" href="/sitemap.xml" />
🧪 Testing
vendor/bin/pest
SQLite must be enabled for in-memory testing.
📄 License
MIT © VeiligLanceren.nl
veiliglanceren/laravel-seo-sitemap 适用场景与选型建议
veiliglanceren/laravel-seo-sitemap 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 961 次下载、GitHub Stars 达 4, 最近一次更新时间为 2025 年 03 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 veiliglanceren/laravel-seo-sitemap 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 veiliglanceren/laravel-seo-sitemap 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 961
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-31