lan143/yii2-yandexturbo

此扩展为 Yandex Turbo 服务生成 RSS 源提供了可能性

安装次数: 1,176

依赖项: 0

建议者: 0

安全: 0

星标: 5

关注者: 3

分支: 5

开放性问题: 2

类型:yii2-extension

0.2.2 2018-01-26 19:52 UTC

This package is not auto-updated.

Last update: 2024-09-20 20:21:16 UTC


README

为 Yandex Turbo 服务自动生成 RSS 2.0 的 Yii2 模块。

安装

安装此扩展的首选方式是通过 composer

  • 运行以下命令之一:
php composer.phar require "lan143/yii2_yandexturbo" "*"

或者

{
  "require": {
    "lan143/yii2_yandexturbo": "*"
  } 
}

将以下内容添加到您的应用 composer.json 文件的 require 部分并运行 composer update

配置

  • 配置您应用配置文件中的 cache 组件,例如
'components' => [
    'cache' => [
        'class' => \yii\caching\FileCache::class,
    ],
]
  • 在您应用配置文件的 modules 部分添加新模块,例如
'modules' => [
    'yandexTurbo' => [
        'class' => \lan143\yii2_yandexturbo\YandexTurbo::class,
        'title' => 'Liftoff News', // not required, default Application name 
        'link' => 'http://liftoff.msfc.nasa.gov/', // not required, default Url::home
        'description' => 'Liftoff to Space Exploration.', // default empty
        'language' => 'en-us', // not required, default Application language
        'elements' => [
            // only model class. Need behavior in model
            \app\models\Records::class,
            // or configuration for creating a behavior
            [
                'class' => \app\models\Records::class,
                'behaviors' => [
                    'yandexTurbo' => [
                        'class' => \lan143\yii2_yandexturbo\YandexTurboBehavior::class,
                        'scope' => function (\yii\db\ActiveQuery $query) {
                            $query->orderBy(['created_at' => SORT_DESC]);
                        },
                        'dataClosure' => function (\app\models\Records $model) {
                            return [
                                'title' => $model->title,
                                'link' => \yii\helpers\Url::to(['records/view', 'id' => $model->id], true),
                                'description' => $model->description,
                                'content' => $model->content,
                                'pubDate' => (new \DateTime($this->created_at))->format(\DateTime::RFC822),
                            ];
                        }
                    ],
                ],
            ],
            // or configure static content
            [
                'title' => 'About page',
                'link' => ['/about'],
                'description' => 'This is about page',
                'content' => 'Some content of about page, will be displayed in Yandex Turbo page. You can use <strong>html<strong> tags.',
                'pubDate' => (new \DateTime('2018-01-26 18:57:00'))->format(\DateTime::RFC822)
            ],
        ],
        'cacheExpire' => 1, // 1 second. Default is 15 minutes
    ],
],
  • 在 AR 模型中添加行为,例如
use DateTime;
use lan143\yii2_yandexturbo\YandexTurboBehavior;
use yii\db\ActiveQuery;
use yii\helpers\Url;

public function behaviors(): array
{
    return [
        'yandexTurbo' => [
            'class' => YandexTurboBehavior::class,
            'scope' => function (ActiveQuery $query) {
                $query->orderBy(['created_at' => SORT_DESC]);
            },
            'dataClosure' => function (self $model) {
                return [
                    'title' => $model->title,
                    'link' => Url::to(['records/view', 'id' => $model->id], true),
                    'description' => $model->description,
                    'content' => $model->content,
                    'pubDate' => (new DateTime($this->created_at))->format(DateTime::RFC822),
                ];
            }
        ],
    ];
}
  • 为您应用配置文件中的 urlManager 添加新规则,例如
'urlManager' => [
    'rules' => [
        ['pattern' => 'yandex_turbo', 'route' => 'yandexTurbo/yandex-turbo/index', 'suffix' => '.xml'],
    ],
],

现在您可以在 Yandex Webmaster 服务中添加 http://siteexample.com/yandex_turbo.xml