简体   繁体   English

yii2:使用 yii2-sitemap-module

[英]yii2: working with yii2-sitemap-module

i used https://github.com/himiklab/yii2-sitemap-module in my yii2 project我在我的 yii2 项目中使用了https://github.com/himiklab/yii2-sitemap-module

this is my console :这是我的控制台:

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,
];

this is my web.php:这是我的 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',
        ],

this is my news controller :这是我的新闻控制器:

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'],
                ],
            ],
        ];
    }

Where is my xml file(url)??我的 xml 文件(url)在哪里?? What change should I do in my code?我应该在我的代码中做哪些更改?

If your controller ( sitemap/default/index ) is work well.如果您的控制器( sitemap/default/index )运行良好。

Your sitemap must be created in root directory via sitemap.xml file name, and accessible from http://your-domain/sitemap.xml URL.您的站点地图必须通过sitemap.xml文件名在根目录中创建,并且可以从http://your-domain/sitemap.xml URL 访问。

For change it refer to this your code:对于更改,请参阅您的代码:

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

A shorter version of the routing that you can use:您可以使用的较短版本的路由:

'rules' => [


                'sitemap.xml' => 'sitemap/default/index',

And relative to this route url in web: http://website/sitemap.xml并且相对于 web 中的此路由 url: http://website/sitemap.xml

However, you can generate a sitemap without any extensions, which simplifies your work and does not need to understand other people's code.但是,您可以生成没有任何扩展的站点地图,这简化了您的工作,并且不需要了解其他人的代码。 To do this, simply create the controller as in my working example:为此,只需按照我的工作示例创建控制器:

<?php


namespace frontend\controllers;

use frontend\models\blog\articles\BlogArticles;
use frontend\models\blog\categories\BlogCategories;
use frontend\models\blog\series\BlogSeries;
use frontend\models\blog\tags\BlogTags;
use yii\web\Controller;
use yii\db\Query;
use Yii;

class SitemapController extends Controller
{

    public function actionIndex()
    {
//if You want delete cache
//        Yii::$app->cache->delete('sitemap');

        if (!$xml_sitemap = Yii::$app->cache->get('sitemap')) {  // if has cache sitemap
            $urls = array();

            // all my categories
            $articles = BlogArticles::find()->active()->orderCreatedAt()->all();
            foreach ($articles as $article) {
                $urls[] = array(
                    'loc' => $article->url,
                    'lastmod' => date( DATE_W3C, strtotime($article->lastMod) ),
                    'changefreq' => 'daily',
                    'priority' => 1.0
                );
            }


            $categories = BlogCategories::find()->orderId()->all();
            foreach ($categories as $category) {
                $urls[] = array(
                    'loc' => $category->url,
                    'changefreq' => 'weekly',
                    'priority' => 0.8
                );
            }

            $series = BlogSeries::find()->orderId()->all();
            foreach ($series as $sery) {
                $urls[] = array(
                    'loc' => $sery->url,
                    'changefreq' => 'weekly',
                    'priority' => 0.5
                );
            }

            $tags = BlogTags::find()->orderId()->all();
            foreach ($tags as $tag) {
                $urls[] = array(
                    'loc' => $tag->url,
                    'changefreq' => 'weekly',
                    'priority' => 0.4
                );
            }



            $xml_sitemap = $this->renderPartial('index', array( 
                'host' => Yii::$app->request->hostInfo,         // your current domain
                'urls' => $urls,                                // с generate urls for sitemap
            ));

            Yii::$app->cache->set('sitemap', $xml_sitemap, 60*60*12); //cache 12 h
        }

        Yii::$app->response->format = \yii\web\Response::FORMAT_XML; 

        echo $xml_sitemap;
    }
}

And You can see result in live site: https://coderius.biz.ua/sitemap.xml您可以在实时站点中看到结果: https : //coderius.biz.ua/sitemap.xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM