merlinus1/php-sitemap
Composer 安装命令:
composer require merlinus1/php-sitemap
包简介
Framework-agnostic Sitemap generator for PHP, Laravel, and Symfony.
README 文档
README
php-sitemap is a modern, framework-agnostic PHP package for generating sitemaps in XML, TXT, HTML, and Google News formats. It works seamlessly with Laravel, Symfony, or any PHP project. Features include high test coverage, robust CI, extensible adapters, and support for images, videos, translations, alternates, and Google News.
Features
- Framework-agnostic: Use in Laravel, Symfony, or any PHP project
- Multiple formats: XML, TXT, HTML, Google News, mobile
- Rich content: Supports images, videos, translations, alternates, Google News
- Modern PHP: Type-safe, extensible, and robust
- High test coverage: 100% code coverage, CI/CD ready
- Easy integration: Simple API, drop-in for controllers/routes
- Extensible: Adapters for Laravel, Symfony, and more
Installation
composer require rumenx/php-sitemap
Usage
Laravel Example
Controller method:
use Rumenx\Sitemap\Sitemap; public function sitemap(Sitemap $sitemap) { $sitemap->add('https://example.com/', now(), '1.0', 'daily'); $sitemap->add('https://example.com/about', now(), '0.8', 'monthly', images: [ ['url' => 'https://example.com/img/about.jpg', 'title' => 'About Us'] ]); // Add more items as needed... return response($sitemap->render('xml'), 200, ['Content-Type' => 'application/xml']); }
Route registration:
Route::get('/sitemap.xml', [SitemapController::class, 'sitemap']);
Advanced:
// Add with translations, videos, alternates, Google News $sitemap->add( 'https://example.com/news', now(), '0.7', 'weekly', images: [['url' => 'https://example.com/img/news.jpg', 'title' => 'News Image']], title: 'News Article', translations: [['language' => 'fr', 'url' => 'https://example.com/fr/news']], videos: [['title' => 'News Video', 'description' => 'Video description']], googlenews: [ 'sitename' => 'Example News', 'language' => 'en', 'publication_date' => now(), ], alternates: [['media' => 'print', 'url' => 'https://example.com/news-print']] );
Symfony Example
Controller:
use Rumenx\Sitemap\Sitemap; use Symfony\Component\HttpFoundation\Response; class SitemapController { public function sitemap(): Response { $sitemap = new Sitemap(); $sitemap->add('https://example.com/', (new \DateTime())->format(DATE_ATOM), '1.0', 'daily'); $sitemap->add('https://example.com/contact', (new \DateTime())->format(DATE_ATOM), '0.5', 'monthly'); // Add more items as needed... return new Response($sitemap->render('xml'), 200, ['Content-Type' => 'application/xml']); } }
Route registration:
# config/routes.yaml sitemap: path: /sitemap.xml controller: App\Controller\SitemapController::sitemap
Generic PHP Example
require 'vendor/autoload.php'; use Rumenx\Sitemap\Sitemap; $sitemap = new Sitemap(); $sitemap->add('https://example.com/', date('c'), '1.0', 'daily'); $sitemap->add('https://example.com/products', date('c'), '0.9', 'weekly', [ ['url' => 'https://example.com/img/product.jpg', 'title' => 'Product Image'] ]); header('Content-Type: application/xml'); echo $sitemap->render('xml');
Advanced Features
// Add with all supported fields $sitemap->add( 'https://example.com/news', date('c'), '0.8', 'daily', images: [['url' => 'https://example.com/img/news.jpg', 'title' => 'News Image']], title: 'News Article', translations: [['language' => 'fr', 'url' => 'https://example.com/fr/news']], videos: [['title' => 'News Video', 'description' => 'Video description']], googlenews: [ 'sitename' => 'Example News', 'language' => 'en', 'publication_date' => date('c'), ], alternates: [['media' => 'print', 'url' => 'https://example.com/news-print']] ); // Render as TXT file_put_contents('sitemap.txt', $sitemap->render('txt')); // Render as HTML file_put_contents('sitemap.html', $sitemap->render('html'));
add() vs addItem()
You can add sitemap entries using either the add() or addItem() methods:
add() — Simple, type-safe, one-at-a-time:
// Recommended for most use cases $sitemap->add( 'https://example.com/', date('c'), '1.0', 'daily', images: [['url' => 'https://example.com/img.jpg', 'title' => 'Image']], title: 'Homepage' );
addItem() — Advanced, array-based, supports batch:
// Add a single item with an array (all fields as keys) $sitemap->addItem([ 'loc' => 'https://example.com/about', 'lastmod' => date('c'), 'priority' => '0.8', 'freq' => 'monthly', 'title' => 'About Us', 'images' => [['url' => 'https://example.com/img/about.jpg', 'title' => 'About Us']], ]); // Add multiple items at once (batch add) $sitemap->addItem([ [ 'loc' => 'https://example.com/page1', 'title' => 'Page 1', ], [ 'loc' => 'https://example.com/page2', 'title' => 'Page 2', ], ]);
- Use
add()for simple, explicit, one-at-a-time additions (recommended for most users). - Use
addItem()for advanced, batch, or programmatic additions with arrays (e.g., when looping over database results).
Testing
./vendor/bin/pest
License
merlinus1/php-sitemap 适用场景与选型建议
merlinus1/php-sitemap 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 06 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「generator」 「php」 「xml」 「Sitemap」 「html」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 merlinus1/php-sitemap 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 merlinus1/php-sitemap 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 merlinus1/php-sitemap 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Memio's PrettyPrinter, used to generate PHP code from given Model
The bundle for easy using json-rpc api on your project
Bundle Symfony DaplosBundle
Alfabank REST API integration
Phalcon Model Generator
Auto-generate dummy data with realistic relationships per Model-like config classes
统计信息
- 总下载量: 20
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-06-17