Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 55 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,16 @@ class ArticlesUrlBuilder implements UrlBuilder
}

// link to section
yield Url::create(
'https://example.com/article/',
$section_update_at ?: new \DateTimeImmutable('-1 day'),
ChangeFrequency::daily(),
9
);
if ($section_update_at !== null) {
yield Url::createSmart('https://example.com/article/', $section_update_at);
} else {
yield Url::create(
'https://example.com/article/',
new \DateTimeImmutable('-1 day'),
ChangeFrequency::daily(),
9
);
}
}
}
```
Expand Down Expand Up @@ -401,6 +405,26 @@ $stream->pushSitemap(new Sitemap('https://example.com/sitemap_articles.xml', new
$stream->close();
```

Result `sitemap.xml`:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>https://example.com/sitemap_main.xml</loc>
<lastmod>2020-06-15T13:39:46+03:00</lastmod>
</sitemap>
<sitemap>
<loc>https://example.com/sitemap_news.xml</loc>
<lastmod>2020-06-15T13:39:46+03:00</lastmod>
</sitemap>
<sitemap>
<loc>https://example.com/sitemap_articles.xml</loc>
<lastmod>2020-06-15T13:39:46+03:00</lastmod>
</sitemap>
</sitemapindex>
```

## Split URLs and make Sitemap index

You can simplify splitting the list of URLs to partitions and creating a Sitemap index.
Expand Down Expand Up @@ -452,14 +476,8 @@ $stream = new WritingSplitIndexStream(
$stream->open();

// build sitemap.xml index file and sitemap1.xml, sitemap2.xml, sitemapN.xml with URLs
$i = 0;
foreach ($builders as $url) {
$stream->push($url);

// not forget free memory
if (++$i % 100 === 0) {
gc_collect_cycles();
}
}

// you can add a link to a sitemap created earlier
Expand All @@ -475,6 +493,31 @@ sitemap.xml
sitemap1.xml
sitemap2.xml
sitemap3.xml
sitemap_news.xml
```

Result `sitemap.xml`:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>https://example.com/sitemap1.xml</loc>
<lastmod>2020-06-15T13:39:46+03:00</lastmod>
</sitemap>
<sitemap>
<loc>https://example.com/sitemap2.xml</loc>
<lastmod>2020-06-15T13:39:46+03:00</lastmod>
</sitemap>
<sitemap>
<loc>https://example.com/sitemap3.xml</loc>
<lastmod>2020-06-15T13:39:46+03:00</lastmod>
</sitemap>
<sitemap>
<loc>https://example.com/sitemap_news.xml</loc>
<lastmod>2020-06-15T13:39:46+03:00</lastmod>
</sitemap>
</sitemapindex>
```

## Split URLs in groups
Expand Down