netglue/sitemap-builder 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

netglue/sitemap-builder

Composer 安装命令:

composer require netglue/sitemap-builder

包简介

A Simple XML Sitemap generation utility

README 文档

README

Why?

It's pretty easy to make an XML sitemap and there's lots of utilities out there, but these tend to concentrate on persisting the xml to disk. If you're running your app on a handful of servers, disks are a pain in the ass.

I wanted something that only generated the XML as a string so that I could easily throw that at a cache, or serialise to files on AWS or Google cloud or whatever.

Install

Install with composer in the normal way…

composer require netglue/sitemap-builder

This lib requires the xml writer extension, and a minimum of PHP 7.1

Tests

Coverage is currently 100%, just CD to the project directory and…

composer install
vendor/bin/phpunit

Usage

Single, standalone sitemap

If you have no need of a sitemap index file, i.e. you're never going to have more than 50k URLs, then something like this should suffice:

use Netglue\Sitemap\Sitemap;

$sitemap = new Sitemap('some-file.xml', 'https://base-host-and-scheme.com');
$sitemap->addUri('/somewhere');

$xml = (string) $sitemap; // or $sitemap->toXmlString()

// Now do whatever with your xml…

The signature for Sitemap::addUri() is:

Sitemap::addUri(
    string $uri,
    ?\DateTimeInterface $lastMod = null,
    ?string $changeFreq = null,
    ?float $priority = null
) : void;

Multiple sitemaps with an index

Adding urls to an index will automatically generate new sitemaps when the max location count is reached for each sitemap. Each sitemap will be named sitemap-{index}.xml where {index} starts at zero

use Netglue\Sitemap\SitemapIndex;
use Netglue\Sitemap\Sitemap;

$index = new SitemapIndex('https://baseurl.host');
// Optionally limit sitemap size (Default is 50k)
$index->setMaxEntriesPerSitemap(10);
// Add a shed load of relative, or absolute URIs
$index->addUri('/someplace');
// ... 

// Retrieve the sitemaps and do something with them:
foreach ($index->getSitemaps() as $sitemap) {
    /** @var Sitemap $sitemap */
    $xml = (string) $sitemap;
    $filename = $sitemap->getName(); // i.e 'sitemap-0.xml'
}

// Do something with the Index XML
$indexXml = (string) $index;

Writing to Disk

I figured that if anyone uses this lib, there's a good chance that they may wish to write sitemaps to disk, so it also includes a simple writer class

use Netglue\Sitemap\Writer\FileWriter;
use Netglue\Sitemap\SitemapIndex;

$writer = new FileWriter('/path/to/disk/location');
$writer->writeIndex($index);

The above will write a Sitemap Index and all the sitemaps found to the given directory. You can provide a filename too instead of the default sitemap-index.xml. If you are working with a single sitemap, then this will do the job:

use Netglue\Sitemap\Writer\FileWriter;
use Netglue\Sitemap\Sitemap;

$writer = new FileWriter('/path/to/disk/location');
$writer->writeSitemap($sitemap, 'my-sitemap.xml');

The filename is optional and defaults to $sitemap->getName()

Exceptions

Consistent Exceptions are thrown that all implement Netglue\Sitemap\Exception\ExceptionInterface. For example, changefreq, when provided must be valid according to the schema. Priority must be a float between 0 and 1 etc.

License

This lib is MIT Licensed. Do what you like with it, but don't blame anyone if there's a problem

Feedback and Contributions…

… are welcomed. Please add tests for any fixes or feature pull requests.

netglue/sitemap-builder 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 132.54k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 26
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-01-03