addItem() public method

Adds a new item to sitemap
public addItem ( string $location, integer $lastModified = null, float $changeFrequency = null, string $priority = null )
$location string location item URL
$lastModified integer last modification timestamp
$changeFrequency float change frequency. Use one of self:: constants here
$priority string item's priority (0.0-1.0). Default null is equal to 0.5
Ejemplo n.º 1
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);
 }
Ejemplo n.º 2
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.');
 }
Ejemplo n.º 3
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!');
 }
Ejemplo n.º 4
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();
 }
Ejemplo n.º 5
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.');
 }
Ejemplo n.º 6
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('Страница не найдена');
     }
 }
Ejemplo n.º 7
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();
 }
Ejemplo n.º 8
0
 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}");
 }
 /**
  * @param Sitemap $sitemap
  * @param $route
  * @param array $parametersCollection
  * @param array $routeConfigurations
  */
 protected function addItems(Sitemap $sitemap, $route, array $parametersCollection, array $routeConfigurations)
 {
     $progress = new ProgressBar($this->output, count($parametersCollection));
     $progress->start();
     foreach ($parametersCollection as $parameters) {
         $sitemap->addItem($this->router->generate($route, $this->accessor->getValue($parameters, '[route_params]') ?: $parameters, true), $this->accessor->getValue($parameters, '[sitemap_optional_tags][lastmod]') ?: $routeConfigurations['lastmod'], $this->accessor->getValue($parameters, '[sitemap_optional_tags][changefreq]') ?: $routeConfigurations['changefreq'], $this->accessor->getValue($parameters, '[sitemap_optional_tags][priority]') ?: $routeConfigurations['priority']);
         $progress->advance();
     }
     $progress->finish();
     $this->output->writeln('');
 }
Ejemplo n.º 10
0
 protected function addItem($url, $strength)
 {
     $this->sitemap->addItem($url, time(), Sitemap::DAILY, $strength);
     $this->info('"' . $url . '/" added to the sitemap!');
 }