write() public method

Finishes writing
public write ( )
Ejemplo n.º 1
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $posts = Post::all(['slug', 'updated_at']);
     $works = Work::all(['slug', 'updated_at']);
     $this->sitemap = new Sitemap(public_path('sitemap.xml'));
     $this->comment('Starting generating the Sitemap...');
     // Home
     $this->addItem(config('app.url') . '/', 0.9);
     // Resume
     $this->addItem(config('app.url') . '/resume', 0.8);
     // Blog index
     $this->addItem(config('app.url') . '/blog', 0.8);
     // Works index
     $this->addItem(config('app.url') . '/works', 0.8);
     // @codeCoverageIgnoreStart
     // Blog posts
     foreach ($posts as $post) {
         $this->addItem(config('app.url') . '/blog/' . $post->slug, 0.7);
     }
     // Works items
     foreach ($works as $work) {
         $this->addItem(config('app.url') . '/works/' . $work->slug, 0.7);
     }
     // @codeCoverageIgnoreEnd
     $this->sitemap->write();
     $this->comment('Sitemap generated successfully!');
 }
Ejemplo n.º 2
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.º 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();
 }
Ejemplo n.º 4
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.º 5
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);
 }
Ejemplo n.º 6
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.º 7
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.º 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}");
 }