Download the PHP package yii2tech/sitemap without Composer
On this page you can find all versions of the php package yii2tech/sitemap. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package sitemap
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
or add
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:
In case you put sitemap generation into a console command, you will need to manually configure URL manager parameters for it. For example:
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:
Once all site map files are generated, you can compose index file, using the following code:
Note: by default site map files are stored under the path '@app/web/sitemap'. If you need a different file path you should adjust
fileBasePath
field 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:
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:
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:
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:
For adding videos to the sitemap entry use 'videos' option. For example:
You can also add any custom content to the URL tag using 3rd argument of the \yii2tech\sitemap\File::writeUrl()
method.
For example:
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.