2017 © Pedro Peláez
 

yii2-extension yii2-sitemap

Yii2 sitemap extension

image

mrssoft/yii2-sitemap

Yii2 sitemap extension

  • Tuesday, April 26, 2016
  • by mrs2000
  • Repository
  • 3 Watchers
  • 10 Stars
  • 888 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 6 Forks
  • 2 Open issues
  • 1 Versions
  • 3 % Grown

The README.md

Sitemap Extension for Yii 2

Latest Stable Version PHP Github Total Downloads, (*1)

Installation

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

Either run, (*3)

php composer.phar require mrssoft/yii2-sitemap "^2.0"

or add, (*4)

"mrssoft/yii2-sitemap": "^2.0"

to the require section of your composer.json., (*5)

Usage

Create controller SitemapController.php, (*6)

<?php

namespace app\controllers;

use \mrssoft\sitemap\Sitemap;

class SitemapController extends \mrssoft\sitemap\SitemapController
{
    /**
     * @var int Cache duration, set null to disabled
     */
    protected $cacheDuration = 43200; // default 12 hour

    /**
     * @var string Cache filename
     */
    protected $cacheFilename = 'sitemap.xml';

    protected $enablePriority = false;

    protected $enableChangeFreq = false;    

    public function models(): array
    {
        return [
            [
                'class' => \app\models\Page::class,
                'change' => Sitemap::MONTHLY,
                'priority' => 0.8,
                'lastmod' => 'updated_at',
            ]
        ];
    }

    public function urls(): array
    {
        return [
            [
                'url' => ['about/index'],
                'priority' => 0.8
            ]
        ];
    }
}

Add to your models interface \mrssoft\sitemap\SitemapInterface, (*7)

<?php
namespace app\models;

class Page extends \yii\db\ActiveRecord implements \mrssoft\sitemap\SitemapInterface
{
    ...

    /**
     * @return \yii\db\ActiveQuery
     */        
    public static function sitemap(): \yii\db\ActiveQuery
    {
        return self::find()->where(['public' => 1]);
    }

    /**
     * @return string
     */
    public function getSitemapUrl(): string
    {
        return \yii\helpers\Url::toRoute(['page/view', 'url' => $this->url], true);
    }    
}

Add to config url rule., (*8)

'components' => [
    'urlManager' => [
        'rules' => [
            ...
            [
                'pattern' => 'sitemap', 
                'route' => 'sitemap/index', 
                'suffix' => '.xml'
            ],
            ...
        ]
    ],

The Versions

26/04 2016

dev-master

9999999-dev

Yii2 sitemap extension

  Sources   Download

MIT

The Requires

 

by Melnikov Ruslan

extension yii2 sitemap