Author: Alexander Makarov (sam@rmcreative.ru)
Example #1
0
 public function actionGenerateSitemap()
 {
     $sitemap = new Sitemap(Yii::getAlias('@app/web') . '/sitemap.xml');
     $posts = Post::findAll(['status' => 1]);
     foreach ($posts as $post) {
         $sitemap->addItem(Url::to(['/blog/post/view', 'slug' => $post->slug], true), time(), Sitemap::DAILY);
     }
     $sitemap->write();
 }
Example #2
0
 public function testFrequencyValidation()
 {
     $this->setExpectedException('InvalidArgumentException');
     $fileName = __DIR__ . '/sitemap.xml';
     $sitemap = new Sitemap($fileName);
     $sitemap->addItem('http://example.com/mylink1');
     $sitemap->addItem('http://example.com/mylink2', time(), 'invalid');
     unlink($fileName);
 }
Example #3
0
 /**
  * @param $sitemapFile
  */
 public function generate($sitemapFile, array $data)
 {
     if (!empty($data)) {
         $this->data = array_merge($this->data, $data);
     }
     $sitemap = new Sitemap($sitemapFile);
     Yii::app()->eventManager->fire(SiteMapEvents::BEFORE_GENERATE, new SiteMapBeforeGenerateEvent($this));
     foreach ($this->data as $item) {
         $sitemap->addItem($item['location'], $item['lastModified'], $item['changeFrequency'], $item['priority']);
     }
     $sitemap->write();
 }
Example #4
0
 public function testLocationValidation()
 {
     $fileName = __DIR__ . '/sitemap.xml';
     $sitemap = new Sitemap($fileName);
     $exceptionCaught = false;
     try {
         $sitemap->addItem('http://example.com/mylink1');
         $sitemap->addItem('notlink', time());
     } catch (\InvalidArgumentException $e) {
         $exceptionCaught = true;
     }
     unlink($fileName);
     $this->assertTrue($exceptionCaught, 'Expected InvalidArgumentException wasn\'t thrown.');
 }
 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.');
 }
Example #6
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $posts = $this->getPosts();
     $sitemap = new Sitemap(public_path('sitemap.xml'));
     $sitemap->addItem(config('app.url') . '/', time(), Sitemap::DAILY, 0.9);
     $sitemap->addItem(config('app.url') . '/resume', time(), Sitemap::DAILY, 0.8);
     $sitemap->addItem(config('app.url') . '/blog', time(), Sitemap::DAILY, 0.8);
     $sitemap->addItem(config('app.url') . '/works', time(), Sitemap::DAILY, 0.8);
     // @codeCoverageIgnoreStart
     foreach ($posts as $post) {
         $sitemap->addItem(config('app.url') . '/' . $post->type . '/' . $post->slug, $post->updated_at->timestamp, Sitemap::DAILY, 0.7);
     }
     // @codeCoverageIgnoreEnd
     $sitemap->write();
     $this->comment('Sitemap generated successfully!');
 }
 protected function generate($language)
 {
     $directory = public_path() . "/{$language}";
     if (!file_exists($directory)) {
         $this->fs->makeDirectory($directory);
     }
     $xml = $directory . "/sitemap.xml";
     $sitemap = new Sitemap($xml);
     $sitemap->addItem(url('/'), time(), Sitemap::DAILY);
     $sitemap->addItem(url('about'), time(), Sitemap::MONTHLY);
     $sitemap->addItem(url('ambassador'), time(), Sitemap::MONTHLY);
     $sitemap->addItem(route('blog'), time(), Sitemap::DAILY);
     $loader = new PostLoader($language);
     $posts = $loader->loadList();
     foreach ($posts as $post) {
         $sitemap->addItem(route('post', $post['slug']), time(), Sitemap::DAILY);
     }
     $tags = (new Tag($language))->all(1000);
     foreach ($tags as $tag => $count) {
         $sitemap->addItem(route('tag', $tag), time(), Sitemap::DAILY);
     }
     $sitemap->write();
     $this->info("Sitemap for site language {$language} was generated and saved to {$xml}");
 }
Example #8
0
 public function actionSitemap()
 {
     if (Yii::$app->request->isPost) {
         $sitemap = new Sitemap(Yii::getAlias('@webroot/sitemap.xml'));
         $sitemap->addItem(Url::to(['/main/default/index'], true), time(), Sitemap::WEEKLY, 1);
         $sitemap->addItem(Url::to(['/main/contact/index'], true), time(), Sitemap::MONTHLY, 1);
         $pages = Page::findAll(['status' => 1]);
         foreach ($pages as $page) {
             $sitemap->addItem(Url::to(['/page/node/view', 'slug' => $page->slug], true), $page->updated_at, Sitemap::MONTHLY, 1);
         }
         $posts = Post::findAll(['status' => 1]);
         foreach ($posts as $post) {
             $sitemap->addItem(Url::to(['/post/node/view', 'slug' => $post->slug], true), $post->updated_at, Sitemap::NEVER, 0.8);
         }
         $sitemap->write();
         Yii::$app->session->setFlash('success', "Файл sitemap.xml успешно обновлен.");
         Yii::$app->request->referrer ? $this->redirect(Yii::$app->request->referrer) : $this->goHome();
     } else {
         throw new NotFoundHttpException('Страница не найдена');
     }
 }
 /**
  * @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;
 }
Example #10
0
 protected function addItem($url, $strength)
 {
     $this->sitemap->addItem($url, time(), Sitemap::DAILY, $strength);
     $this->info('"' . $url . '/" added to the sitemap!');
 }