getSitemapUrls() public method

Returns an array of URLs written
public getSitemapUrls ( string $baseUrl ) : array
$baseUrl string base URL of all the sitemaps written
return array URLs of sitemaps written
Exemplo n.º 1
0
 public function testMultipleFiles()
 {
     $sitemap = new Sitemap(__DIR__ . '/sitemap_multi.xml');
     $sitemap->setMaxUrls(2);
     for ($i = 0; $i < 20; $i++) {
         $sitemap->addItem('http://example.com/mylink' . $i, time());
     }
     $sitemap->write();
     $expectedFiles = [__DIR__ . '/' . 'sitemap_multi.xml', __DIR__ . '/' . 'sitemap_multi_2.xml', __DIR__ . '/' . 'sitemap_multi_3.xml', __DIR__ . '/' . 'sitemap_multi_4.xml', __DIR__ . '/' . 'sitemap_multi_5.xml', __DIR__ . '/' . 'sitemap_multi_6.xml', __DIR__ . '/' . 'sitemap_multi_7.xml', __DIR__ . '/' . 'sitemap_multi_8.xml', __DIR__ . '/' . 'sitemap_multi_9.xml', __DIR__ . '/' . 'sitemap_multi_10.xml'];
     foreach ($expectedFiles as $expectedFile) {
         $this->assertTrue(file_exists($expectedFile), "{$expectedFile} does not exist!");
         unlink($expectedFile);
     }
     $urls = $sitemap->getSitemapUrls('http://example.com/');
     $this->assertEquals(10, count($urls), print_r($urls, true));
     $this->assertContains('http://example.com/sitemap_multi.xml', $urls);
     $this->assertContains('http://example.com/sitemap_multi_10.xml', $urls);
 }
Exemplo n.º 2
0
 public function actionGenerate()
 {
     $webUrl = Yii::getAlias('@frontendUrl');
     // проверка наличия слеша в конце ссылки
     if (!StringHelper::endsWith($webUrl, '/', false)) {
         $webUrl .= '/';
     }
     $webPath = Yii::getAlias('@frontend/web/');
     // create sitemap
     $sitemap = new Sitemap($webPath . 'sitemap.xml');
     // add some URLs
     foreach (Article::find()->published()->all() as $item) {
         $sitemap->addItem($webUrl . 'article/' . $item->slug, time(), Sitemap::DAILY);
     }
     // write it
     $sitemap->write();
     // get URLs of sitemaps written
     $sitemapFileUrls = $sitemap->getSitemapUrls($webUrl);
     // create sitemap for static files
     $staticSitemap = new Sitemap($webPath . 'sitemap_static.xml');
     // add some URLs
     $staticSitemap->addItem($webUrl . 'article/index');
     $staticSitemap->addItem($webUrl . 'site/contact');
     // write it
     $staticSitemap->write();
     // get URLs of sitemaps written
     $staticSitemapUrls = $staticSitemap->getSitemapUrls($webUrl);
     // create sitemap index file
     $index = new Index($webPath . 'sitemap_index.xml');
     // add URLs
     foreach ($sitemapFileUrls as $sitemapUrl) {
         $index->addSitemap($sitemapUrl);
     }
     // add more URLs
     foreach ($staticSitemapUrls as $sitemapUrl) {
         $index->addSitemap($sitemapUrl);
     }
     // write it
     $index->write();
     Console::output('The sitemap generated successfully.');
 }
 /**
  * @param Sitemap $sitemap
  * @param array   $options
  *
  * @return Index
  */
 protected function generateSitemapsIndex(Sitemap $sitemap, array $options)
 {
     $sitemapIndex = new Index($options['index']['path']);
     $sitemapFileUrls = $sitemap->getSitemapUrls($this->getBaseUrl($options));
     foreach ($sitemapFileUrls as $sitemapUrl) {
         $sitemapIndex->addSitemap($sitemapUrl);
     }
     return $sitemapIndex;
 }