yii2tech/sitemap
最新稳定版本:1.1.1
Composer 安装命令:
composer require yii2tech/sitemap
包简介
Provides support for site map creation
README 文档
README
Site Map Extension for Yii 2
This extension provides support for site map and site map index files generating.
For license information check the LICENSE-file.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist yii2tech/sitemap or add
"yii2tech/sitemap": "*"
to the require section of your composer.json.
Usage
This extension provides support for site map and site map index files generation. You can use \yii2tech\sitemap\File for site map file composition:
<?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();
In case you put sitemap generation into a console command, you will need to manually configure URL manager parameters for it. For example:
<?php return [ 'id' => 'my-console-application', 'components' => [ 'urlManager' => [ 'hostInfo' => 'https://example.com', 'baseUrl' => '/', 'scriptUrl' => '/index.php', ], // ... ], // ... ];
Creating site map index files
There is a limitation on the site map maximum size. Such file can not contain more then 50000 entries and its actual size can not exceed 50MB. If you web application has more then 50000 pages and you need to generate site map for it, you'll have to split it between several files and then generate a site map index file. It is up to you how you split your URLs between different site map files, however you can use \yii2tech\sitemap\File::getEntriesCount() or \yii2tech\sitemap\File::getIsEntriesLimitReached() method to check count of already written entries.
For example: assume we have an 'item' table, which holds several millions of records, each of which has a detail view page at the web application. In this case generating site map files for such pages may look like following:
<?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); } }
Once all site map files are generated, you can compose index file, using the following code:
<?php use yii2tech\sitemap\IndexFile; $siteMapIndexFile = new IndexFile(); $siteMapIndexFile->writeUp();
Note: by default site map files are stored under the path '@app/web/sitemap'. If you need a different file path you should adjust
fileBasePathfield accordingly.
Rendering on-the-fly
Saving sitemap to the physical file may be not a best option to keep it up-to-date. Such file should be manually re-created, once some changes among site pages appear. You may setup a web controller, which will render 'sitemap.xml' file on demand, once it is been requested. This controller may apply caching and its busting logic. First of all, you'll have to set up a route for the controller action rendering the sitemap in your URL manager. For example:
<?php return [ 'components' => [ 'urlManager' => [ 'rules' => [ 'sitemap.xml' => 'site/sitemap', // ... ], ], // ... ], // ... ];
Then you'll need to create an action, which will render sitemap file content and emit it to the web client. You can use PHP 'in memory' stream as a file name for the sitemap file during its composition. The final implementation may look like following:
<?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; } }
Customizing file envelope
You can customize entries envelope for the particular file using following options:
-
\yii2tech\sitemap\BaseFile::$header- content, which should be written at the beginning of the file, once it has been opened; -
\yii2tech\sitemap\BaseFile::$footer- content, which should be written at the end of the file before it is closed; -
\yii2tech\sitemap\BaseFile::$rootTag- defines XML root tag name and attributes;
For example:
<?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();
Rendering non-standard tags
While there is a standard, which defines sitemap content, particular search engines may accept extra tags and options. The most widely used are image and video descriptions. Method \yii2tech\sitemap\File::writeUrl() supports rendering image and video information.
For adding images to the sitemap entry use 'images' option. For example:
<?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();
For adding videos to the sitemap entry use 'videos' option. For example:
<?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();
You can also add any custom content to the URL tag using 3rd argument of the \yii2tech\sitemap\File::writeUrl() method. For example:
<?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();
Heads up! Remember that you'll have to add corresponding XML namespaces to the sitemap file, using \yii2tech\sitemap\BaseFile::$rootTag, in order to non-standard tags being recognized by the search engines.
yii2tech/sitemap 适用场景与选型建议
yii2tech/sitemap 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 91.89k 次下载、GitHub Stars 达 59, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Sitemap」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yii2tech/sitemap 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yii2tech/sitemap 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yii2tech/sitemap 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Symfony sitemap generator
A custom URL rule class for Yii 2 which allows to create translated URL rules
PHP Simple Sitemap Generator
Interfaces for web resource models and services to retrieve and create them
Sitemap crawler/generator. For the given URL it will return sitemap XML file with URLs and images.
Sitemap and sitemap index builder for Jigsaw.
统计信息
- 总下载量: 91.89k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 63
- 点击次数: 14
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2026-01-04