Creating an RSS with Yii 2

Hi everyone,

I have searched a lot to find a tutorial on how to create an RSS action using Yii2.

I don’t want to use any extensions and I want to do it from scratch. I couldn’t find any tutorials. Could anyone help me with this or share a link so that I can do it myself?

Thanks alot!

RSS is basically XML. So you need to foreach content and form XML then serve it.

Thank you for your reply. I have created a controller and a view. I query my content then I will send them to the view to put the right content between the right XML tag. After rendering my view, however, chrome shows the result like a normal HTML not an XML file.

That’s content type header missing.

Yii::$app->response->getHeaders()->set('Content-Type', 'application/rss+xml');

Thanks, but it didn’t work.

This is my code:


public function actionFeed()


            Yii::$app->response->getHeaders()->set('Content-Type', 'application/rss+xml');

            $posts = Drlifestyle::find()

                ->joinWith(['node'], false)

                ->with(['node', 'cover', 'body', 'tag'])


                    'node.status' 	=> 1,

                    'node.type'   	=> 'drlifestyle'


            return $this->renderPartial('feed', [

                'posts' => $posts,




<rss version="2.0">


        <title>سلامتیسم | سبک زندگی پزشکان</title>

        <atom:link href="" rel="self" type="application/rss+xml"/>


        <description>مجله آنلاین سلامت</description>


        <copyright>كلیه حقوق مادی و معنوی این وب سایت، متعلق به «سلامتیسم» است.</copyright>

        <?php if (!empty($posts)): ?>

            <?php foreach ($posts as $post): ?>


                    <title><?php echo \yii\helpers\Html::encode(Yii::$app->publicFunctions->echoP(trim($post['node']->title))); ?></title>

                    <description><?php echo \yii\helpers\HtmlPurifier::process(Yii::$app->publicFunctions->echoP(strip_tags($post['body']->summary))); ?></description>

                    <?php if (!empty($post['tag'])): ?>

                        <?php foreach ($post['tag'] as $tag): ?>

                            #<?php echo \yii\helpers\Html::encode(Yii::$app->publicFunctions->echoP(trim($tag->tag))); ?>

                        <?php endforeach; ?>

                    <?php endif; ?>


                        $title   = Yii::$app->publicFunctions->echoP($post['node']->title);

                        $pic_uri = Yii::$app->publicFunctions->getImageUrlByFid(

                            (!empty($post['cover'])) ? $post['cover']->fid : 0, 200, 200);


                    <img src="<?php echo Yii::$app->params['domainName'].$pic_uri; ?>" alt="<?php echo $title ?>"/>

                    <link><?php echo Yii::$app->params['domainName']. '/?n=' . $post->nid; ?></link>


            <?php endforeach; ?>

        <?php endif; ?>



This is the result:


Forgot one other line which is:

Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;

Thanks a lot. You really helped me.

Thank you very very much

Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;

Do you put this in the actionFeed() function? Above or Below the ->getHeaders()?

Yes. Action function. Order doesn’t matter.