PHP code example of indexnowkit / yii2

1. Go to this page and download the library: Download indexnowkit/yii2 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

indexnowkit / yii2 example snippets


// config/web.php and config/console.php
'bootstrap' => ['indexnow'],                   // registers the console controller and the key file route
'components' => [
    'indexnow' => [
        'class' => \IndexNowKit\Yii2\IndexNowComponent::class,
        'options' => [
            'key' => getenv('INDEXNOW_KEY'),
            'base_url' => 'https://www.example.com',   // used by console commands and queue workers
            'dry_run' => YII_ENV_DEV,                  // dev/staging: log the request, send nothing (check fails when this is unset outside production)
        ],
    ],
],

use IndexNowKit\Attribute\{IndexNow, IndexNowDefaults};
use IndexNowKit\Yii2\ActiveRecord\IndexNowBehavior;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;

#[IndexNowDefaults(when: 'published', fields: ['slug', 'title', 'body', 'published'])]
#[IndexNow(route: 'post/view', params: ['slug' => 'slug'])]
#[IndexNow(route: 'post/amp', params: ['slug' => 'slug'], when: 'amp')]
#[IndexNow(via: 'category')]      // a changed post also refreshes its category page
#[IndexNow(urls: ['/'])]          // and the homepage
final class Post extends ActiveRecord
{
    public static function tableName(): string
    {
        return 'posts';
    }

    public function init(): void
    {
        parent::init();
        $this->loadDefaultValues();   // `published` has a database default: make it visible before the first save
    }

    public function behaviors(): array
    {
        return [IndexNowBehavior::class];
    }

    public function getCategory(): ActiveQuery
    {
        return $this->hasOne(Category::class, ['id' => 'category_id']);
    }
}

'indexnow' => ['class' => IndexNowComponent::class, 'options' => [
    // ...
    'history' => [
        'store' => 'pdo',                // null (default, nothing kept) | psr16 (the debounce cache component) | pdo
        'pdo' => ['service' => 'db'],    // the db component holding the table — or 'dsn' => 'sqlite:/var/data/indexnow.sqlite'
    ],
]],

use IndexNowKit\Attribute\{IndexNow, IndexNowDefaults};
use IndexNowKit\Yii2\ActiveRecord\IndexNowBehavior;

#[IndexNowDefaults(when: 'published', fields: ['slug', 'title', 'published'])]
#[IndexNow(route: 'post/view', params: ['slug' => 'slug'])]
#[IndexNow(urls: ['/'])]
final class Post extends ActiveRecord { public function behaviors(): array { return [IndexNowBehavior::class]; } }