2017 © Pedro Peláez
 

yii2-extension yii2-sitemap

A Yii2 tool to generate sitemap.xml.

image

zhelyabuzhsky/yii2-sitemap

A Yii2 tool to generate sitemap.xml.

  • Thursday, September 7, 2017
  • by zhelyabuzhsky
  • Repository
  • 4 Watchers
  • 22 Stars
  • 12,995 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 9 Forks
  • 0 Open issues
  • 9 Versions
  • 10 % Grown

The README.md

Sitemap.xml generator for Yii2

Build Status Total Downloads, (*1)

Yii2 extension to generate sitemap files for large web-sites through Yii2 console command, (*2)

Installation

The preferred way to install this extension is through composer., (*3)

``` bash $ php composer.phar require zhelyabuzhsky/yii2-sitemap, (*4)


or add

"zhelyabuzhsky/yii2-sitemap": "^1.1", (*5)


to the require section of your `composer.json` file. ## Features * Generates multiple sitemaps (large sites) * Creates index sitemap file * Gzip compression of .xml files * Disallow urls support (through regular expression array) ## Configuration ### 1. Configure `urlManager` at console config ```php 'urlManager' => [ 'hostInfo' => 'https://example.com', 'baseUrl' => '/', 'rules' => [ // ... ], ],

NOTE Both params hostInfo and baseUrl are required for Yii2 console app., (*6)

NOTE urlManager rules section usually repeats your frontend urlManager configuration, so you could merge it at console config (see https://github.com/yiisoft/yii2/issues/1578#issuecomment-66716648):, (*7)

Show details
, (*8)

console/main.php, (*9)

$frontendUrlManager = require(__DIR__ . '/../../frontend/config/UrlManager.php');
//...
'urlManager' => array_merge($frontendUrlManager, [
    'hostInfo' => 'https://example.com'
]),

frontend/config/UrlManager.php, (*10)

 '/',
    'class' => 'yii\web\UrlManager',
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
      //...
    ],
];

?>

, (*11)

2. Configure Sitemap component at console config components section

'components' => [
  'sitemap' => [
    'class' => '\zhelyabuzhsky\sitemap\components\Sitemap',
  ],
],

Example of using extra Sitemap params, (*12)

'components' => [
  'sitemap' => [
    'class' => '\zhelyabuzhsky\sitemap\components\Sitemap',
    'maxUrlsCountInFile' => 10000,
    'sitemapDirectory' => 'frontend/web',
    'optionalAttributes' => ['changefreq', 'lastmod', 'priority'],
    'maxFileSize' => '10M',
  ],
 ],

where * maxUrlsCountInFile - max count of urls in one sitemap file; * sitemapDirectory - directory to place sitemap files; * optionalAttributes - list of used optional attributes; * maxFileSize - maximal file size. Zero to work without limits. So you can specify the following abbreviations k - kilobytes and m - megabytes. By default 10m., (*13)

Usage

1. Impement SitemapEntityInterface for the models you want to use at sitemap

Show example
, (*14)

common\models\Category.php, (*15)

use yii\db\ActiveRecord;
use zhelyabuzhsky\sitemap\models\SitemapEntityInterface;

class Category extends ActiveRecord implements SitemapEntityInterface
{
    /**
     * @inheritdoc
     */
    public function getSitemapLastmod()
    {
        return date('c');
    }
    /**
     * @inheritdoc
     */
    public function getSitemapChangefreq()
    {
        return 'daily';
    }
    /**
     * @inheritdoc
     */
    public function getSitemapPriority()
    {
        return 0.5;
    }
    /**
     * @inheritdoc
     */
    public function getSitemapLoc()
    {
        // Use urlManager rules to create urls
        return $url = Yii::$app->urlManager->createAbsoluteUrl([
            'page/view',
            'pageSlug' => $this->slug,
        ]);
        // or directly
        // return 'http://localhost/' . $this->slug;
    }
    /**
     * @inheritdoc
     */
    public static function getSitemapDataSource()
    {
        return self::find();
    }
}

, (*16)

2. Create Yii2 controller for console command

use yii\console\Controller;

class SitemapController extends Controller
{
  public function actionCreate()
  {
    \Yii::$app->sitemap
      ->addModel(Item::className())
      ->addModel(Category::className(), \Yii::$app->db) // Also you can pass \yii\db\Connection to the database connection that you need to use
      ->setDisallowUrls([
        '#url1#',
        '#url2$#',
      ])
      ->create();
    }
}

3. Run console command

php yii sitemap/create

Testing

Set enviroment variable SERVER_NAME (e.g. https://example.com), (*17)

bash $ ./vendor/bin/phpunit, (*18)

Security

If you discover any security related issues, please email zhelyabuzhsky@icloud.com instead of using the issue tracker., (*19)

Credits

License

GNU General Public License, version 3. Please see License File for more information., (*20)

The Versions

07/09 2017

dev-master

9999999-dev https://github.com/zhelyabuzhsky/yii2-sitemap

A Yii2 tool to generate sitemap.xml.

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Ilya Zhelyabuzhsky

yii2 sitemap

07/09 2017

1.1.3

1.1.3.0 https://github.com/zhelyabuzhsky/yii2-sitemap

A Yii2 tool to generate sitemap.xml.

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Ilya Zhelyabuzhsky

yii2 sitemap

07/09 2017

dev-PaVeL-Ekt-multy-base

dev-PaVeL-Ekt-multy-base https://github.com/zhelyabuzhsky/yii2-sitemap

A Yii2 tool to generate sitemap.xml.

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Ilya Zhelyabuzhsky

yii2 sitemap

08/12 2016

1.1.2

1.1.2.0 https://github.com/zhelyabuzhsky/yii2-sitemap

A Yii2 tool to generate sitemap.xml.

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Ilya Zhelyabuzhsky

yii2 sitemap

07/12 2016

dev-chernoskutov/filesize-limit

dev-chernoskutov/filesize-limit https://github.com/zhelyabuzhsky/yii2-sitemap

A Yii2 tool to generate sitemap.xml.

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Ilya Zhelyabuzhsky

yii2 sitemap

06/12 2016

1.1.1

1.1.1.0 https://github.com/zhelyabuzhsky/yii2-sitemap

A Yii2 tool to generate sitemap.xml.

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Ilya Zhelyabuzhsky

yii2 sitemap

25/02 2016

1.1.0

1.1.0.0 https://github.com/zhelyabuzhsky/yii2-sitemap

A Yii2 tool to generate sitemap.xml.

  Sources   Download

GPL-2.0

The Requires

 

The Development Requires

by Ilya Zhelyabuzhsky

yii2 sitemap

22/07 2015

1.0.1

1.0.1.0 https://github.com/zhelyabuzhsky/yii2-sitemap

A Yii2 tool to generate sitemap.xml.

  Sources   Download

GPL-2.0

The Requires

 

The Development Requires

by Ilya Zhelyabuzhsky

yii2 sitemap

20/06 2015

1.0.0

1.0.0.0 https://github.com/zhelyabuzhsky/yii2-sitemap

A Yii2 tool to generate sitemap.xml.

  Sources   Download

GPL-2.0

The Requires

 

The Development Requires

by Ilya Zhelyabuzhsky

yii2 sitemap