承接 jdz/sitemap 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jdz/sitemap

Composer 安装命令:

composer require jdz/sitemap

包简介

JDZ Sitemap generator

README 文档

README

A modern PHP library for generating XML sitemaps and sitemap indexes compliant with the sitemaps.org protocol.

Features

  • 🚀 Easy to use - Simple and intuitive API
  • 📦 Modern PHP - Built for PHP 8.1+ with type safety
  • 🔒 Type-safe enums - Use the Frequency enum for change frequency values
  • 🎯 Standards compliant - Follows sitemaps.org protocol specifications
  • 📝 Automatic handling - Manages large sitemaps (splits after 40,000 URLs)
  • 🔄 Sitemap index support - Create sitemap indexes for multiple sitemaps
  • Fully tested - Comprehensive test suite

Requirements

  • PHP: ^8.1
  • Extensions:
    • ext-simplexml

Installation

Install via Composer:

composer require jdz/sitemap

Quick Start

Basic Sitemap

<?php

require_once 'vendor/autoload.php';

use JDZ\Sitemap\Map;
use JDZ\Sitemap\Url;
use JDZ\Sitemap\Frequency;

// Create a sitemap
$sitemap = new Map('/path/to/public', 'sitemap', 'https://example.com');

// Add URLs
$sitemap->addItem(new Url('/', 'now', Frequency::DAILY, 0.9));
$sitemap->addItem(new Url('/about', 'now', Frequency::WEEKLY, 0.8));
$sitemap->addItem(new Url('/contact', 'now', Frequency::MONTHLY, 0.7));

// Write sitemap to file
$sitemap->write();

This generates: /path/to/public/sitemap/sitemap.xml

Sitemap Index

<?php

require_once 'vendor/autoload.php';

use JDZ\Sitemap\Index;
use JDZ\Sitemap\Map;
use JDZ\Sitemap\Group;
use JDZ\Sitemap\Url;
use JDZ\Sitemap\Frequency;

$publicPath = '/path/to/public';

// Create sitemap index
$index = new Index($publicPath);

// Create main sitemap
$sitemap = new Map($publicPath, 'sitemap', 'https://example.com');
$sitemap->addItem(new Url('/', 'now', Frequency::DAILY, 0.9));
$sitemap->addItem(new Url('/blog', 'now', Frequency::DAILY, 0.8));
$sitemap->write();

// Add to index
foreach ($sitemap->writtenFilePaths as $path) {
    $index->addItem(new Group('https://example.com/sitemap/' . $path));
}

// Create subdomain sitemap
$subSitemap = new Map($publicPath, 'subdomain', 'https://blog.example.com');
$subSitemap->addItem(new Url('/', 'now', Frequency::DAILY, 0.9));
$subSitemap->addItem(new Url('/posts', 'now', Frequency::WEEKLY, 0.8));
$subSitemap->write();

// Add to index
foreach ($subSitemap->writtenFilePaths as $path) {
    $index->addItem(new Group('https://example.com/sitemap/' . $path));
}

// Write index
$index->write();

This generates:

  • /path/to/public/sitemap.xml (index)
  • /path/to/public/sitemap/sitemap.xml
  • /path/to/public/sitemap/subdomain.xml

Frequency Enum Cases

The Frequency enum provides type-safe change frequency values:

use JDZ\Sitemap\Frequency;

Frequency::ALWAYS   // 'always'
Frequency::HOURLY   // 'hourly'
Frequency::DAILY    // 'daily'
Frequency::WEEKLY   // 'weekly'
Frequency::MONTHLY  // 'monthly'
Frequency::YEARLY   // 'yearly'
Frequency::NEVER    // 'never'

Examples

See the examples directory for detailed examples:

  • example.php - Complete usage demonstration with all verbosity levels

Creating a URL with all parameters

use JDZ\Sitemap\Url;
use JDZ\Sitemap\Frequency;

$url = new Url(
    '/products/item-123',           // Location (path)
    '2023-12-07 10:30:00',         // Last modification date
    Frequency::WEEKLY,              // Change frequency
    0.8                             // Priority (0.0 to 1.0)
);

Using default values

// Only path is required, other parameters have defaults:
// - lastmod: 'now' (current timestamp)
// - changefreq: Frequency::WEEKLY
// - priority: 0.5

$url = new Url('/simple-page');

Handling duplicate URLs

Duplicate URLs are automatically ignored:

$sitemap->addItem(new Url('/page1'));
$sitemap->addItem(new Url('/page1')); // Ignored
$sitemap->addItem(new Url('/page2'));
// Only 2 URLs will be in the sitemap

Automatic file splitting

The library automatically creates multiple sitemap files when you exceed 40,000 URLs:

$sitemap = new Map($publicPath, 'sitemap', 'https://example.com');

for ($i = 1; $i <= 100000; $i++) {
    $sitemap->addItem(new Url("/page-{$i}"));
}

$sitemap->write();

// Generates:
// - sitemap/sitemap.xml (URLs 1-40,000)
// - sitemap/sitemap-2.xml (URLs 40,001-80,000)
// - sitemap/sitemap-3.xml (URLs 80,001-100,000)

// Access generated files
print_r($sitemap->writtenFilePaths);
// Array: ['sitemap.xml', 'sitemap-2.xml', 'sitemap-3.xml']

Testing

Run the test suite:

# Run all tests
composer test

# Run with coverage
composer test -- --coverage-html coverage

# Run specific test file
vendor/bin/phpunit tests/OutputTest.php
vendor/bin/phpunit tests/VerbosityTest.php

# Run with detailed output
vendor/bin/phpunit --testdox

License

This library is licensed under the MIT License. See the LICENSE file for details.

Changelog

Version 2.0.0

Breaking Changes:

  • Minimum PHP version increased to 8.1+
  • Added type declarations throughout the codebase

New Features:

  • ✨ Added Frequency enum for type-safe change frequency values
  • 📦 Full PHP 8.1+ compatibility with union types and enums
  • ✅ Comprehensive test suite

Improvements:

  • 🎯 Modern PHP practices and strict typing
  • 📝 Enhanced error handling with proper exceptions

Maintenance:

  • 🔧 Updated dependencies
  • 📚 Complete README with examples and documentation
  • ✨ PSR-4 autoloading for tests

jdz/sitemap 适用场景与选型建议

jdz/sitemap 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 31 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 03 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「Sitemap」 「utilities」 「services」 「JDZ」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 jdz/sitemap 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 jdz/sitemap 我们能提供哪些服务?
定制开发 / 二次开发

基于 jdz/sitemap 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 31
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 14
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-05