Coder Social home page Coder Social logo

yii2-sitemap-module's Introduction

XML Sitemap Module for Yii2

PHP version Scrutinizer Code Quality Code Coverage Build Status

Yii2 module for automatically generating XML Sitemap.

Installation

The preferred way to install this extension is through composer.

  • Either run
php composer.phar require --prefer-dist "assayer-pro/yii2-sitemap-module" "*"

or add

"assayer-pro/yii2-sitemap-module" : "*"

to the require section of your application's composer.json file.

  • Configure the cache component of your application's configuration file, for example:
'components' => [
    'cache' => [
        'class' => 'yii\caching\FileCache',
    ],
]
  • Add a new module in modules section of your application's configuration file, for example:
'modules' => [
    'sitemap' => [
        'class' => 'assayerpro\sitemap\Module',
    ],
...
],
  • Add confuguration for sitemap into components section:
'components' => [
    'robotsTxt' => [
        'class' => 'assayerpro\sitemap\RobotsTxt',
        'userAgent' => [
            // Disallow url for all bots
            '*' => [
                'Disallow' => [
                    ['/api/default/index'],
                ],
                'Allow' => [
                    ['/api/doc/index'],
                ],
            ],
            // Block a specific image from Google Images
            'Googlebot-Image' => [
                'Disallow' => [
                    // All images on your site from Google Images
                    '/',
                    // Files of a specific file type (for example, .gif)
                    '/*.gif$',
                ],
            ],
        ],
    ],
    'sitemap' => [
        'class' => 'assayerpro\sitemap\Sitemap',
        'models' => [
            // your models
            'app\modules\news\models\News',
            // or configuration for creating a behavior
            [
                'class' => 'app\modules\news\models\News',
                'behaviors' => [
                    'sitemap' => [
                        'class' => '\assayerpro\sitemap\behaviors\SitemapBehavior',
                        'scope' => function ($model) {
                            /** @var \yii\db\ActiveQuery $model */
                            $model->select(['url', 'lastmod']);
                            $model->andWhere(['is_deleted' => 0]);
                        },
                        'dataClosure' => function ($model) {
                            /** @var self $model */
                            return [
                                'loc' => Url::to($model->url, true),
                                'lastmod' => strtotime($model->lastmod),
                                'changefreq' => \assayerpro\sitemap\Sitemap::DAILY,
                                'priority' => 0.8
                            ];
                        }
                    ],
                ],
            ],
        ],
        'urls'=> [
            // your additional urls
            [
                'loc' => ['/news/default/index'],
                'changefreq' => \assayerpro\sitemap\Sitemap::DAILY,
                'priority' => 0.8,
                'news' => [
                    'publication'   => [
                        'name'          => 'Example Blog',
                        'language'      => 'en',
                    ],
                    'access'            => 'Subscription',
                    'genres'            => 'Blog, UserGenerated',
                    'publication_date'  => 'YYYY-MM-DDThh:mm:ssTZD',
                    'title'             => 'Example Title',
                    'keywords'          => 'example, keywords, comma-separated',
                    'stock_tickers'     => 'NASDAQ:A, NASDAQ:B',
                ],
                'images' => [
                    [
                        'loc'           => 'http://example.com/image.jpg',
                        'caption'       => 'This is an example of a caption of an image',
                        'geo_location'  => 'City, State',
                        'title'         => 'Example image',
                        'license'       => 'http://example.com/license',
                    ],
                ],
            ],
        ],
        'enableGzip' => true, // default is false
        'cacheExpire' => 1, // 1 second. Default is 24 hours
    ],
],
  • Add behavior in the AR models, for example:
use assayerpro\sitemap\behaviors\SitemapBehavior;

public function behaviors()
{
    return [
        'sitemap' => [
            'class' => SitemapBehavior::className(),
            'scope' => function ($model) {
                /** @var \yii\db\ActiveQuery $model */
                $model->select(['url', 'lastmod']);
                $model->andWhere(['is_deleted' => 0]);
            },
            'dataClosure' => function ($model) {
                /** @var self $model */
                return [
                    'loc' => Url::to($model->url, true),
                    'lastmod' => strtotime($model->lastmod),
                    'changefreq' => Sitemap::DAILY,
                    'priority' => 0.8
                ];
            }
        ],
    ];
}
  • Add a new rule for urlManager of your application's configuration file, for example:
'urlManager' => [
    'rules' => [
        ['pattern' => 'sitemap-<id:\d+>', 'route' => '/sitemap/default/index', 'suffix' => '.xml'],
        ['pattern' => 'sitemap', 'route' => 'sitemap/default/index', 'suffix' => '.xml'],
    ],
],

Console generate sitemap

Remove sitemap section from modules configuration.

Add console command configuration:

    'controllerMap' => [
        'sitemap' => [
            'class' => 'assayerpro\sitemap\console\CreateController',
        ],
    ],

Add baseUrl for urlManager:

     'urlManager' => [
         'baseUrl' => '',
         'hostInfo' => 'http://example.com/',
     ],

Run command from console:

$ ./yii sitemap/create

Resources

yii2-sitemap-module's People

Contributors

serge-larin avatar himiklab avatar execut avatar scrutinizer-auto-fixer avatar ladamalina avatar chuprik avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.