{"id":15169496,"url":"https://github.com/yii2tech/sitemap","last_synced_at":"2025-10-01T02:31:22.004Z","repository":{"id":57087046,"uuid":"38429215","full_name":"yii2tech/sitemap","owner":"yii2tech","description":"Site map creation support","archived":true,"fork":false,"pushed_at":"2019-10-17T11:04:09.000Z","size":87,"stargazers_count":58,"open_issues_count":0,"forks_count":18,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-09-22T22:01:57.464Z","etag":null,"topics":["sitemap","sitemap-generator","yii","yii2","yii2-extension"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yii2tech.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["klimov-paul"],"patreon":"klimov_paul"}},"created_at":"2015-07-02T11:34:22.000Z","updated_at":"2024-09-11T15:37:03.000Z","dependencies_parsed_at":"2022-08-25T00:50:49.836Z","dependency_job_id":null,"html_url":"https://github.com/yii2tech/sitemap","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yii2tech%2Fsitemap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yii2tech%2Fsitemap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yii2tech%2Fsitemap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yii2tech%2Fsitemap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yii2tech","download_url":"https://codeload.github.com/yii2tech/sitemap/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219875269,"owners_count":16554660,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["sitemap","sitemap-generator","yii","yii2","yii2-extension"],"created_at":"2024-09-27T07:02:21.207Z","updated_at":"2025-10-01T02:31:16.679Z","avatar_url":"https://github.com/yii2tech.png","language":"PHP","funding_links":["https://github.com/sponsors/klimov-paul","https://patreon.com/klimov_paul"],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/yii2tech\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://avatars2.githubusercontent.com/u/12951949\" height=\"100px\"\u003e\n    \u003c/a\u003e\n    \u003ch1 align=\"center\"\u003eSite Map Extension for Yii 2\u003c/h1\u003e\n    \u003cbr\u003e\n\u003c/p\u003e\n\nThis extension provides support for site map and site map index files generating.\n\nFor license information check the [LICENSE](LICENSE.md)-file.\n\n[![Latest Stable Version](https://img.shields.io/packagist/v/yii2tech/sitemap.svg)](https://packagist.org/packages/yii2tech/sitemap)\n[![Total Downloads](https://img.shields.io/packagist/dt/yii2tech/sitemap.svg)](https://packagist.org/packages/yii2tech/sitemap)\n[![Build Status](https://travis-ci.org/yii2tech/sitemap.svg?branch=master)](https://travis-ci.org/yii2tech/sitemap)\n\n\nInstallation\n------------\n\nThe preferred way to install this extension is through [composer](http://getcomposer.org/download/).\n\nEither run\n\n```\nphp composer.phar require --prefer-dist yii2tech/sitemap\n```\n\nor add\n\n```json\n\"yii2tech/sitemap\": \"*\"\n```\n\nto the require section of your composer.json.\n\n\nUsage\n-----\n\nThis extension provides support for site map and site map index files generation.\nYou can use `\\yii2tech\\sitemap\\File` for site map file composition:\n\n```php\n\u003c?php\n\nuse yii2tech\\sitemap\\File;\n\n$siteMapFile = new File();\n\n$siteMapFile-\u003ewriteUrl(['site/index'], ['priority' =\u003e '0.9']);\n$siteMapFile-\u003ewriteUrl(['site/about'], ['priority' =\u003e '0.8', 'changeFrequency' =\u003e File::CHECK_FREQUENCY_WEEKLY]);\n$siteMapFile-\u003ewriteUrl(['site/signup'], ['priority' =\u003e '0.7', 'lastModified' =\u003e '2015-05-07']);\n$siteMapFile-\u003ewriteUrl(['site/contact']);\n\n$siteMapFile-\u003eclose();\n```\n\nIn case you put sitemap generation into a console command, you will need to manually configure URL manager\nparameters for it. For example:\n\n```php\n\u003c?php\n\nreturn [\n    'id' =\u003e 'my-console-application',\n    'components' =\u003e [\n        'urlManager' =\u003e [\n            'hostInfo' =\u003e 'https://example.com',\n            'baseUrl' =\u003e '/',\n            'scriptUrl' =\u003e '/index.php',\n        ],\n        // ...\n    ],\n    // ...\n];\n```\n\n\n## Creating site map index files \u003cspan id=\"creating-site-map-index-files\"\u003e\u003c/span\u003e\n\nThere is a limitation on the site map maximum size. Such file can not contain more then 50000 entries and its\nactual size can not exceed 50MB. If you web application has more then 50000 pages and you need to generate\nsite map for it, you'll have to split it between several files and then generate a site map index file.\nIt is up to you how you split your URLs between different site map files, however you can use `\\yii2tech\\sitemap\\File::getEntriesCount()`\nor `\\yii2tech\\sitemap\\File::getIsEntriesLimitReached()` method to check count of already written entries.\n\nFor example: assume we have an 'item' table, which holds several millions of records, each of which has a detail\nview page at the web application. In this case generating site map files for such pages may look like following:\n\n```php\n\u003c?php\n\nuse app\\models\\Item;\nuse yii2tech\\sitemap\\File;\n\n$query = Item::find()-\u003eselect(['slug'])-\u003easArray();\n\n$siteMapFileCount = 0;\nforeach ($query-\u003eeach() as $row) {\n    if (empty($siteMapFile)) {\n        // if there is no active file - create one with unique name:\n        $siteMapFile = new File();\n        $siteMapFileCount++;\n        $siteMapFile-\u003efileName = 'item_' . $siteMapFileCount . '.xml';\n    }\n\n    $siteMapFile-\u003ewriteUrl(['item/view', 'slug' =\u003e $row['slug']]);\n    if ($siteMapFile-\u003egetIsEntriesLimitReached()) {\n        // once file is full - close it, allowing creating a new one at the next cycle iteration:\n        unset($siteMapFile);\n    }\n}\n```\n\nOnce all site map files are generated, you can compose index file, using the following code:\n\n```php\n\u003c?php\n\nuse yii2tech\\sitemap\\IndexFile;\n\n$siteMapIndexFile = new IndexFile();\n$siteMapIndexFile-\u003ewriteUp();\n```\n\n\u003e Note: by default site map files are stored under the path '@app/web/sitemap'. If you need a different file path\n  you should adjust `fileBasePath` field accordingly.\n\n\n## Rendering on-the-fly \u003cspan id=\"rendering-on-the-fly\"\u003e\u003c/span\u003e\n\nSaving sitemap to the physical file may be not a best option to keep it up-to-date. Such file should be manually re-created,\nonce some changes among site pages appear. You may setup a web controller, which will render 'sitemap.xml' file on demand,\nonce it is been requested. This controller may apply caching and its busting logic.\nFirst of all, you'll have to set up a route for the controller action rendering the sitemap in your URL manager. For example:\n\n```php\n\u003c?php\n\nreturn [\n    'components' =\u003e [\n        'urlManager' =\u003e [\n            'rules' =\u003e [\n                'sitemap.xml' =\u003e 'site/sitemap',\n                // ...\n            ],\n        ],\n        // ...\n    ],\n    // ...\n];\n```\n\nThen you'll need to create an action, which will render sitemap file content and emit it to the web client.\nYou can use PHP 'in memory' stream as a file name for the sitemap file during its composition.\nThe final implementation may look like following:\n\n```php\n\u003c?php\n\nnamespace app\\controllers;\n\nuse Yii;\nuse yii\\web\\Controller;\nuse yii\\web\\Response;\nuse yii2tech\\sitemap\\File;\n\nclass SiteController extends Controller\n{\n    public function actionSitemap()\n    {\n        // get content from cache:\n        $content = Yii::$app-\u003ecache-\u003eget('sitemap.xml');\n        if ($content === false) {\n            // if no cached value exists - create an new one\n            // create sitemap file in memory:\n            $sitemap = new File();\n            $sitemap-\u003efileName = 'php://memory';\n            \n            // write your site URLs:\n            $sitemap-\u003ewriteUrl(['site/index'], ['priority' =\u003e '0.9']);\n            // ...\n            \n            // get generated content:\n            $content = $sitemap-\u003egetContent();\n\n            // save generated content to cache\n            Yii::$app-\u003ecache-\u003eset('sitemap.xml', $content);\n        }\n\n        // send sitemap content to the user agent:\n        $response = Yii::$app-\u003egetResponse();\n        $response-\u003eformat = Response::FORMAT_RAW;\n        $response-\u003egetHeaders()-\u003eadd('Content-Type', 'application/xml;');\n        $response-\u003econtent = $content;\n        \n        return $response;\n    }\n}\n```\n\n\n## Customizing file envelope \u003cspan id=\"customizing-file-envelope\"\u003e\u003c/span\u003e\n\nYou can customize entries envelope for the particular file using following options:\n\n - `\\yii2tech\\sitemap\\BaseFile::$header` - content, which should be written at the beginning of the file, once it has been opened;\n \n - `\\yii2tech\\sitemap\\BaseFile::$footer` - content, which should be written at the end of the file before it is closed;\n \n - `\\yii2tech\\sitemap\\BaseFile::$rootTag` - defines XML root tag name and attributes;\n \nFor example:\n\n```php\n\u003c?php\n\nuse yii2tech\\sitemap\\File;\n\n$siteMapFile = new File([\n    'header' =\u003e '\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\u003c?xml-stylesheet type=\"text/xsl\" href=\"//example.com/main-sitemap.xsl\"?\u003e',\n    'rootTag' =\u003e [\n        'tag' =\u003e 'urlset',\n        'xmlns' =\u003e 'http://www.sitemaps.org/schemas/sitemap/0.9',\n        'xmlns:xsi' =\u003e 'http://www.w3.org/2001/XMLSchema-instance',\n        'xmlns:image' =\u003e 'http://www.google.com/schemas/sitemap-image/1.1',\n        'xsi:schemaLocation' =\u003e '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',\n    ],\n]);\n\n$siteMapFile-\u003ewriteUrl(['site/index'], ['priority' =\u003e '0.9']);\n// ...\n\n$siteMapFile-\u003eclose();\n```\n\n\n## Rendering non-standard tags \u003cspan id=\"rendering-non-standard-tags\"\u003e\u003c/span\u003e\n\nWhile there is a [standard](http://www.sitemaps.org/), which defines sitemap content, particular search engines may accept\nextra tags and options. The most widely used are image and video descriptions.\nMethod `\\yii2tech\\sitemap\\File::writeUrl()` supports rendering image and video information.\n\nFor adding images to the sitemap entry use 'images' option. For example:\n\n```php\n\u003c?php\n\nuse yii2tech\\sitemap\\File;\n\n$siteMapFile = new File([\n    'rootTag' =\u003e [\n        'xmlns' =\u003e 'http://www.sitemaps.org/schemas/sitemap/0.9',\n        'xmlns:image' =\u003e 'http://www.google.com/schemas/sitemap-image/1.1', // you will need to add XML namespace for non-standard tags\n    ],\n]);\n\n$siteMapFile-\u003ewriteUrl(['site/index'], [\n    'images' =\u003e [\n        [\n            'url' =\u003e 'http://example.com/images/logo.jpg',\n            'title' =\u003e 'Logo',\n        ],\n        [\n            'url' =\u003e 'http://example.com/images/avatars/john-doe.jpg',\n            'title' =\u003e 'Author',\n        ],\n        // ...\n    ],\n]);\n// ...\n\n$siteMapFile-\u003eclose();\n```\n\nFor adding videos to the sitemap entry use 'videos' option. For example:\n\n```php\n\u003c?php\n\nuse yii2tech\\sitemap\\File;\n\n$siteMapFile = new File([\n    'rootTag' =\u003e [\n        'xmlns' =\u003e 'http://www.sitemaps.org/schemas/sitemap/0.9',\n        'xmlns:video' =\u003e 'http://www.google.com/schemas/sitemap-video/1.1', // you will need to add XML namespace for non-standard tags\n    ],\n]);\n\n$siteMapFile-\u003ewriteUrl(['site/index'], [\n    'videos' =\u003e [\n        [\n            'title' =\u003e 'Demo video',\n            'description' =\u003e 'Demo video of the main process',\n            'thumbnailUrl' =\u003e 'http://example.com/images/demo-video.jpg',\n            'player' =\u003e [\n                'url' =\u003e 'http://example.com/videos/demo.flv',\n                'allowEmbed' =\u003e true,\n                'autoplay' =\u003e 'ap=1',\n            ],\n            'publicationDate' =\u003e '2019-08-02',\n            'duration' =\u003e 240,\n        ],\n        [\n            'title' =\u003e 'Our team',\n            'description' =\u003e 'Greetings from our team',\n            'thumbnailUrl' =\u003e 'http://example.com/images/our-team.jpg',\n            'player' =\u003e [\n                'url' =\u003e 'http://example.com/videos/our-team.flv',\n                'allowEmbed' =\u003e true,\n                'autoplay' =\u003e 'ap=1',\n            ],\n            'publicationDate' =\u003e '2019-08-02',\n            'duration' =\u003e 120,\n        ],\n        // ...\n    ],\n]);\n// ...\n\n$siteMapFile-\u003eclose();\n```\n\nYou can also add any custom content to the URL tag using 3rd argument of the `\\yii2tech\\sitemap\\File::writeUrl()` method.\nFor example:\n\n```php\n\u003c?php\n\nuse yii2tech\\sitemap\\File;\n\n$siteMapFile = new File([\n    'rootTag' =\u003e [\n        'xmlns' =\u003e 'http://www.sitemaps.org/schemas/sitemap/0.9',\n        'xmlns:image' =\u003e 'http://www.google.com/schemas/sitemap-image/1.1', // you will need to add XML namespace for non-standard tags\n    ],\n]);\n\n$siteMapFile-\u003ewriteUrl(\n    ['site/index'],\n    [],\n    '\u003cimage:image\u003e\u003cimage:loc\u003ehttp://example.com/images/logo.jpg\u003c/image:loc\u003e\u003c/image:image\u003e'\n);\n// ...\n\n$siteMapFile-\u003eclose();\n```\n\n**Heads up!** Remember that you'll have to add corresponding XML namespaces to the sitemap file, using `\\yii2tech\\sitemap\\BaseFile::$rootTag`,\nin order to non-standard tags being recognized by the search engines.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyii2tech%2Fsitemap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyii2tech%2Fsitemap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyii2tech%2Fsitemap/lists"}