douzhangcuo2174 2016-08-11 08:43
浏览 62

在yii2高级模板中创建rss

I have a problem while creating rss in my yii2 advance template project. I have try to install the zelenin rss extension in my project through adding necessary code in composer.json, but when i try to use it, there is an error: Class 'Zelenin\yii\extensions\Rss\RssView' not found I believe the error is from the controller function that taken from the link. Here is the code :

    public function actionRss()
{
     $searchModel = new EventOrganizerSearch();
            $dataProvider = $searchModel->searchApprovedEvent();

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

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

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

I have follow the guideline in the git hub link (https://github.com/zelenin/yii2-rss) but still fail to use it.

I have been trying for many hours. Can anyone show me a proper way to implement the rss features to my website? Thank you.

  • 写回答

1条回答 默认 最新

  • duan0821 2016-08-11 09:29
    关注

    Try adding

    use Zelenin\yii\extensions\Rss;
    

    to your controller

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?