yii2tech/sitemap

此包已被废弃,不再维护。未建议替代包。

提供网站地图创建支持

资助包维护!
klimov-paul
Patreon

安装次数: 65,560

依赖: 5

建议者: 0

安全: 0

星标: 59

关注者: 7

分支: 14

公开问题: 0

类型:yii2-extension

1.1.1 2019-10-17 11:03 UTC

This package is auto-updated.

Last update: 2022-01-10 10:44:19 UTC


README

12951949

Yii 2 网站地图扩展


此扩展提供网站地图和网站地图索引文件生成支持。

有关许可证信息,请参阅LICENSE文件。

Latest Stable Version Total Downloads Build Status

安装

安装此扩展的首选方法是使用composer

运行以下命令

php composer.phar require --prefer-dist yii2tech/sitemap

或在您的composer.json文件的require部分添加以下内容

"yii2tech/sitemap": "*"

使用

此扩展提供网站地图和网站地图索引文件生成支持。您可以使用\yii2tech\sitemap\File进行网站地图文件组成

<?php

use yii2tech\sitemap\File;

$siteMapFile = new File();

$siteMapFile->writeUrl(['site/index'], ['priority' => '0.9']);
$siteMapFile->writeUrl(['site/about'], ['priority' => '0.8', 'changeFrequency' => File::CHECK_FREQUENCY_WEEKLY]);
$siteMapFile->writeUrl(['site/signup'], ['priority' => '0.7', 'lastModified' => '2015-05-07']);
$siteMapFile->writeUrl(['site/contact']);

$siteMapFile->close();

如果将网站地图生成放入控制台命令中,则需要手动配置URL管理器的参数。例如

<?php

return [
    'id' => 'my-console-application',
    'components' => [
        'urlManager' => [
            'hostInfo' => 'https://example.com',
            'baseUrl' => '/',
            'scriptUrl' => '/index.php',
        ],
        // ...
    ],
    // ...
];

创建网站地图索引文件

网站地图的最大大小有限制。此类文件不能包含超过50000个条目,其实际大小不能超过50MB。如果您有超过50000个页面的Web应用程序,并且需要为其生成网站地图,您将不得不将其分割到多个文件中,然后生成网站地图索引文件。您可以根据自己的需要如何将URL分割到不同的网站地图文件中,但是您可以使用\yii2tech\sitemap\File::getEntriesCount()\yii2tech\sitemap\File::getIsEntriesLimitReached()方法来检查已写入条目的数量。

例如:假设我们有一个'item'表,它包含数百万条记录,每条记录都有一个在Web应用程序中的详细视图页面。在这种情况下,生成此类页面的网站地图文件可能如下所示

<?php

use app\models\Item;
use yii2tech\sitemap\File;

$query = Item::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);
    }
}

一旦生成所有网站地图文件,您可以使用以下代码生成索引文件

<?php

use yii2tech\sitemap\IndexFile;

$siteMapIndexFile = new IndexFile();
$siteMapIndexFile->writeUp();

注意:默认情况下,网站地图文件存储在路径'@app/web/sitemap'下。如果您需要不同的文件路径,您应相应地调整'fileBasePath'字段。

动态渲染

将网站地图保存到物理文件可能不是保持其更新的最佳选项。此类文件应在网站页面出现更改时手动重新创建。您可以设置一个Web控制器,在需要时渲染'sitemap.xml'文件。此控制器可以应用缓存和其清除逻辑。首先,您必须在URL管理器中设置控制器动作渲染网站地图的路线。例如

<?php

return [
    'components' => [
        'urlManager' => [
            'rules' => [
                'sitemap.xml' => 'site/sitemap',
                // ...
            ],
        ],
        // ...
    ],
    // ...
];

然后,您需要创建一个动作,该动作将渲染网站地图文件内容并将其输出到Web客户端。您可以在其组成期间使用PHP 'in memory'流作为网站地图文件的文件名。最终实现可能如下所示

<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use yii\web\Response;
use yii2tech\sitemap\File;

class SiteController extends Controller
{
    public function actionSitemap()
    {
        // get content from cache:
        $content = Yii::$app->cache->get('sitemap.xml');
        if ($content === false) {
            // if no cached value exists - create an new one
            // create sitemap file in memory:
            $sitemap = new File();
            $sitemap->fileName = 'php://memory';
            
            // write your site URLs:
            $sitemap->writeUrl(['site/index'], ['priority' => '0.9']);
            // ...
            
            // get generated content:
            $content = $sitemap->getContent();

            // save generated content to cache
            Yii::$app->cache->set('sitemap.xml', $content);
        }

        // send sitemap content to the user agent:
        $response = Yii::$app->getResponse();
        $response->format = Response::FORMAT_RAW;
        $response->getHeaders()->add('Content-Type', 'application/xml;');
        $response->content = $content;
        
        return $response;
    }
}

自定义文件封装

您可以使用以下选项自定义特定文件的条目信封:

  • \yii2tech\sitemap\BaseFile::$header - 在文件打开时应在文件开头写入的内容;

  • \yii2tech\sitemap\BaseFile::$footer - 在关闭文件之前应在文件末尾写入的内容;

  • \yii2tech\sitemap\BaseFile::$rootTag - 定义XML根标签名称和属性;

例如

<?php

use yii2tech\sitemap\File;

$siteMapFile = new File([
    'header' => '<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="//example.com/main-sitemap.xsl"?>',
    'rootTag' => [
        'tag' => 'urlset',
        'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9',
        'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
        'xmlns:image' => 'http://www.google.com/schemas/sitemap-image/1.1',
        'xsi:schemaLocation' => 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.google.com/schemas/sitemap-image/1.1 http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd',
    ],
]);

$siteMapFile->writeUrl(['site/index'], ['priority' => '0.9']);
// ...

$siteMapFile->close();

渲染非标准标签

尽管有一个定义了站点地图内容的标准,但特定的搜索引擎可能接受额外的标签和选项。最常用的包括图片和视频描述。方法 \yii2tech\sitemap\File::writeUrl() 支持渲染图片和视频信息。

要向站点地图条目添加图片,请使用 'images' 选项。例如

<?php

use yii2tech\sitemap\File;

$siteMapFile = new File([
    'rootTag' => [
        'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9',
        'xmlns:image' => 'http://www.google.com/schemas/sitemap-image/1.1', // you will need to add XML namespace for non-standard tags
    ],
]);

$siteMapFile->writeUrl(['site/index'], [
    'images' => [
        [
            'url' => 'http://example.com/images/logo.jpg',
            'title' => 'Logo',
        ],
        [
            'url' => 'http://example.com/images/avatars/john-doe.jpg',
            'title' => 'Author',
        ],
        // ...
    ],
]);
// ...

$siteMapFile->close();

要向站点地图条目添加视频,请使用 'videos' 选项。例如

<?php

use yii2tech\sitemap\File;

$siteMapFile = new File([
    'rootTag' => [
        'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9',
        'xmlns:video' => 'http://www.google.com/schemas/sitemap-video/1.1', // you will need to add XML namespace for non-standard tags
    ],
]);

$siteMapFile->writeUrl(['site/index'], [
    'videos' => [
        [
            'title' => 'Demo video',
            'description' => 'Demo video of the main process',
            'thumbnailUrl' => 'http://example.com/images/demo-video.jpg',
            'player' => [
                'url' => 'http://example.com/videos/demo.flv',
                'allowEmbed' => true,
                'autoplay' => 'ap=1',
            ],
            'publicationDate' => '2019-08-02',
            'duration' => 240,
        ],
        [
            'title' => 'Our team',
            'description' => 'Greetings from our team',
            'thumbnailUrl' => 'http://example.com/images/our-team.jpg',
            'player' => [
                'url' => 'http://example.com/videos/our-team.flv',
                'allowEmbed' => true,
                'autoplay' => 'ap=1',
            ],
            'publicationDate' => '2019-08-02',
            'duration' => 120,
        ],
        // ...
    ],
]);
// ...

$siteMapFile->close();

您还可以使用 \yii2tech\sitemap\File::writeUrl() 方法的第三个参数将任何自定义内容添加到URL标签中。例如

<?php

use yii2tech\sitemap\File;

$siteMapFile = new File([
    'rootTag' => [
        'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9',
        'xmlns:image' => 'http://www.google.com/schemas/sitemap-image/1.1', // you will need to add XML namespace for non-standard tags
    ],
]);

$siteMapFile->writeUrl(
    ['site/index'],
    [],
    '<image:image><image:loc>http://example.com/images/logo.jpg</image:loc></image:image>'
);
// ...

$siteMapFile->close();

请注意!请记住,您必须在站点地图文件中使用 \yii2tech\sitemap\BaseFile::$rootTag 添加相应的XML命名空间,以便搜索引擎能够识别非标准标签。