grozzzny/sitemap

Sitemap module for Yii2

Installs: 199

Dependents: 3

Suggesters: 0

Security: 0

Stars: 1

Watchers: 2

Forks: 0

Open Issues: 0

Type:yii2-extension

v2.0 2020-08-19 17:19 UTC

This package is auto-updated.

Last update: 2024-11-20 04:14:41 UTC


README

Installation guide

$ php composer.phar require grozzzny/sitemap "v2.0"

Add console config

  'aliases' => [
        '@webroot' => '@app/web',
  ],
   'components' => [
          'urlManager' => [
              'enablePrettyUrl' => true,
              'showScriptName' => false,
              'hostInfo' => 'my-site.ru',
              'scriptUrl' => '',
              'baseUrl' => ''
          ],
      ],
  'modules' => [
        'sitemap' => [
            'class' => 'grozzzny\sitemap\SitemapModule',
            'domain' => 'https://my-site.ru',
            'generatedByLink' => 'https://pr-kenig.ru',
            'generatedByName' => 'PRkenig',
            'controllerMap' => [
                'console' => 'app\commands\SitemapController'
            ]
        ]
    ],

CRON

php yii sitemap/console/update

Example app\commands\SitemapController

class SitemapController extends ConsoleController
{
    public $lastmodStaticPage = ''; // Y-m-d

    public $staticPages = [
        [
            'loc' => '/about', // /about
            'lastmod' => '2020-08-19', // Y-m-d
            'changefreq' => Sitemap::CHANGEFREQ_MONTHLY,
            'priority' => Sitemap::PRIORITY_60,
        ],
    ];

    protected function dataSitemap()
    {
        $this->generateArticles();
    }

    protected function generateArticles()
    {
        $models = AdminArticles::find()
            ->andWhere(['active' => true])
            ->all();

        foreach($models as $model){
            $this->data_sitemap['articles'][] = array(
                'loc'           => $model->link,
                'lastmod'       => Sitemap::lastmodFormat($model->updated_at),
                'changefreq'    => Sitemap::CHANGEFREQ_MONTHLY,
                'priority'      => Sitemap::PRIORITY_60,
            );
        }
    }
}