balrok/yii2-rss

Yii2 RSS 扩展为您的网站添加 RSS-订阅

安装: 130

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 25

开放问题: 0

类型:yii2-extension

0.1.3 2018-06-19 15:39 UTC

This package is not auto-updated.

Last update: 2024-09-20 02:53:06 UTC


README

Yii2 RSS 扩展为您的网站添加 RSS-订阅

这是对 https://github.com/zelenin/yii2-rss 的分支,因为原始作者已经放弃该项目,并且存在发送头部的错误。

安装

Composer

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

运行以下命令:

php composer.phar require balrok/yii2-rss "~0.1"

或者将以下内容添加到您的 composer.json 文件的 require 部分:

"balrok/yii2-rss": "~0.1"

用法

将操作添加到控制器中

public function actionRss()
{
    $dataProvider = new ActiveDataProvider([
        'query' => Post::find()->with(['user']),
        'pagination' => [
            'pageSize' => 10
        ],
    ]);

    $response = Yii::$app->getResponse();
    $headers = $response->getHeaders();

    $headers->set('Content-Type', 'application/rss+xml; charset=utf-8');

    echo \balrok\yii\extensions\Rss\RssView::widget([
        'dataProvider' => $dataProvider,
        'channel' => [
            'title' => function ($widget, \balrok\Feed $feed) {
                    $feed->addChannelTitle(Yii::$app->name);
            },
            'link' => Url::toRoute('/', true),
            'description' => 'Posts ',
            'language' => function ($widget, \balrok\Feed $feed) {
                return Yii::$app->language;
            },
            'image'=> function ($widget, \balrok\Feed $feed) {
                $feed->addChannelImage('http://example.com/channel.jpg', 'http://example.com', 88, 31, 'Image description');
            },
        ],
        'items' => [
            'title' => function ($model, $widget, \balrok\Feed $feed) {
                    return $model->name;
                },
            'description' => function ($model, $widget, \balrok\Feed $feed) {
                    return StringHelper::truncateWords($model->content, 50);
                },
            'link' => function ($model, $widget, \balrok\Feed $feed) {
                    return Url::toRoute(['post/view', 'id' => $model->id], true);
                },
            'author' => function ($model, $widget, \balrok\Feed $feed) {
                    return $model->user->email . ' (' . $model->user->username . ')';
                },
            'guid' => function ($model, $widget, \balrok\Feed $feed) {
                    $date = \DateTime::createFromFormat('Y-m-d H:i:s', $model->updated_at);
                    return Url::toRoute(['post/view', 'id' => $model->id], true) . ' ' . $date->format(DATE_RSS);
                },
            'pubDate' => function ($model, $widget, \balrok\Feed $feed) {
                    $date = \DateTime::createFromFormat('Y-m-d H:i:s', $model->updated_at);
                    return $date->format(DATE_RSS);
                }
        ]
    ]);
}

作者