public function testLocationValidation() { $this->setExpectedException('InvalidArgumentException'); $fileName = __DIR__ . '/sitemap.xml'; $index = new Index($fileName); $index->addSitemap('noturl'); unlink($fileName); }
public function testWritingFile() { $fileName = __DIR__ . '/sitemap_index.xml'; $index = new Index($fileName); $index->addSitemap('http://example.com/sitemap.xml'); $index->addSitemap('http://example.com/sitemap_2.xml', time()); $index->write(); $this->assertTrue(file_exists($fileName)); unlink($fileName); }
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; }