2016-05-28 59 views
1

我用https://github.com/himiklab/yii2-sitemap-module我yii2項目,yii2-網站地圖模塊工作yii2:

這是我的控制檯:

return [ 
    'id' => 'basic-console', 
    'language' => 'fa-IR', 
    'basePath' => dirname(__DIR__), 
    'bootstrap' => ['log', 'gii'], 
    'controllerNamespace' => 'app\commands', 
    'modules' => [ 
     'gii' => 'yii\gii\Module', 
     'user' => [ 
      'class' => 'dektrium\user\Module', 
      'sourceLanguage' => 'en-US', 
      'languages' => 'fa-IR' 
     ], 
     'sitemap' => [ 
      'class' => 'himiklab\sitemap\Sitemap', 
      'models' => [ 
       // your models 
       'app\modules\news\models\News', 
       // or configuration for creating a behavior 
       [ 
        'class' => 'app\modules\news\models\News', 
        'behaviors' => [ 
         'sitemap' => [ 
          'class' => SitemapBehavior::className(), 
          'scope' => function ($model) { 
         /** @var \yii\db\ActiveQuery $model */ 
         $model->select(['url', 'lastmod']); 
         $model->andWhere(['is_deleted' => 0]); 
        }, 
          'dataClosure' => function ($model) { 
         /** @var self $model */ 
         return [ 
          'loc' => Url::to($model->url, true), 
          'lastmod' => strtotime($model->lastmod), 
          'changefreq' => SitemapBehavior::CHANGEFREQ_DAILY, 
          'priority' => 0.8 
         ]; 
        } 
         ], 
        ], 
       ], 
      ], 
      'urls' => [ 
       // your additional urls 
       [ 
        'loc' => '/news/all', 
        'changefreq' => \himiklab\sitemap\behaviors\SitemapBehavior::CHANGEFREQ_DAILY, 
        'priority' => 0.8, 
        'news' => [ 
         'publication' => [ 
          'name' => 'Example Blog', 
          'language' => 'fa', 
         ], 
         'access' => 'Subscription', 
         'genres' => 'Blog, UserGenerated', 
         'publication_date' => 'YYYY-MM-DDThh:mm:ssTZD', 
         'title' => 'Example Title', 
         'keywords' => 'example, keywords, comma-separated', 
         'stock_tickers' => 'NASDAQ:A, NASDAQ:B', 
        ], 
        'images' => [ 
         [ 
          'loc' => 'http://example.com/image.jpg', 
          'caption' => 'This is an example of a caption of an image', 
          'geo_location' => 'City, State', 
          'title' => 'Example image', 
          'license' => 'http://example.com/license', 
         ], 
        ], 
       ], 
      ], 
      'enableGzip' => true, // default is false 
      'cacheExpire' => 1, // 1 second. Default is 24 hours 
     ], 
    ], 
    'components' => [ 
     'cache' => [ 
      'class' => 'yii\caching\FileCache', 
     ], 
     'log' => [ 
      'targets' => [ 
       [ 
        'class' => 'yii\log\FileTarget', 
        'levels' => ['error', 'warning'], 
       ], 
      ], 
     ], 
     'db' => $db, 
    ], 
    'params' => $params, 
]; 

這是我web.php:

'urlManager' => [ 
      'enablePrettyUrl' => TRUE, 
      'showScriptName' => TRUE, 
      'enableStrictParsing' => FALSE, 
      'rules' => [ 
       ['pattern' => 'sitemap', 'route' => 'sitemap/default/index', 'suffix' => '.xml'], 
      // ... 
      ], 
     ], 
     'request' => [ 
      // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation 
      'cookieValidationKey' => 'salt', 
     ], 
     'cache' => [ 
      'class' => 'yii\caching\FileCache', 
     ], 

這是我的新聞控制器:

use himiklab\sitemap\behaviors\SitemapBehavior; 




    public function behaviors() { 
     return [ 

      'sitemap' => [ 
       'class' => SitemapBehavior::className(), 
       'scope' => function ($model) { 
      /** @var \yii\db\ActiveQuery $model */ 
      $model->select(['id']); 
//   $model->andWhere(['is_deleted' => 0]); 
     }, 
       'dataClosure' => function ($model) { 
      /** @var self $model */ 
      return [ 
       'loc' => Url::to($model->url, true), 
       'lastmod' => strtotime($model->lastmod), 
       'changefreq' => SitemapBehavior::CHANGEFREQ_DAILY, 
       'priority' => 0.8 
      ]; 
     } 
      ], 
      'verbs' => [ 
       'class' => VerbFilter::className(), 
       'actions' => [ 
        'delete' => ['get'], 
       ], 
      ], 
     ]; 
    } 

我的xml文件(url)在哪裏? 我應該在我的代碼中做什麼改變?

回答

1

如果你的控制器(sitemap/default/index)是工作得很好。

您的站點地圖必須根目錄通過sitemap.xml文件名來創建,並從http://your-domain/sitemap.xml URL訪問。

對於改變其參考此代碼:

'rules' => [ 
     ['pattern' => 'sitemap', 'route' => 'sitemap/default/index', 'suffix' => '.xml'], 
    ], 
+0

HTTP://localhost/iicitySite/index.php/sitemap.xml是NOTFOUND! – Saltern

+0

@Saltern檢查這個:http://localhost/iicitySite/sitemap.xml –