😅Site Map, sitemap phongkhamthienhoa.vn (ok)

https://github.com/yii2tech/sitemap

Đọc thêm: https://github.com/DanaLuther/ESitemap

https://github.com/demisang/yii2-sitemap-generator

C:\xampp72\htdocs\testvn\modules\fontend\controllers\SitemapController.php

<?php
namespace app\modules\fontend\controllers;
use app\modules\fontend\controllers\Controllers;
use Yii;
use yii2tech\sitemap\File;
use app\models\Category;
class SitemapController extends controllers
{
  /**
   * Displays sitemap in XML or HTML format,
   * depending on the value of $format parameter
   * @param string $format
   */
  public function actionIndex($format = 'xml')
  {
    $query = Category::find()->select(['slug'])->asArray();
    $siteMapFileCount = 0;
    foreach ($query->each() as $row) {
      if (empty($siteMapFile)) {
        // if there is no active file - create one with unique name:
        $siteMapFile = new File();
        $siteMapFileCount++;
        $siteMapFile->fileName = 'item_' . $siteMapFileCount . '.xml';
      }
      $siteMapFile->writeUrl(['item/view', 'slug' => $row['slug']]);
      if ($siteMapFile->getIsEntriesLimitReached()) {
        // once file is full - close it, allowing creating a new one at the next cycle iteration:
        unset($siteMapFile);
      }
    }
    // $siteMapFile = new File();
    // $siteMapFile->writeUrl(['site/index'], ['priority' => '0.9']);
    // $siteMapFile->close();
    // if (!$xml_sitemap = Yii::$app->cache->get('sitemap')) {
    //   $xml_sitemap = $this->renderPartial('sitemap');
    //   Yii::$app->cache->set('sitemap', $xml_sitemap, 60 * 60 * 12);
    // }
    // $headers = Yii::$app->response->headers;
    // $headers->add('Content-Type', 'text/xml');
    // return $xml_sitemap;
  }
  public function actionSitemap()
  {
    if (!$xml_sitemap = Yii::$app->cache->get('sitemap')) {
      $xml_sitemap = $this->renderPartial('sitemap');
      Yii::$app->cache->set('sitemap', $xml_sitemap, 60 * 60 * 12);
    }
    $headers = Yii::$app->response->headers;
    $headers->add('Content-Type', 'text/xml');
    return $xml_sitemap;
  }
}

C:\xampp72\htdocs\testvn\config\web.php

'rules' => [
    'rss.html' => 'fontend/rss/index',
   'sitemap.xml' => 'fontend/rss/index',
]

Last updated

Was this helpful?