Sitemap.xml creation

Hello. I am new in Yii2. I want to create sitemap for my site. I created controller where I define all the urls of my site.


public function actionIndex()


		$urls = [];

                // ....

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

		return $this->renderPartial('index', ['urls' => $urls]);




/* @var $this yii\web\View */

/* @var $urls */

echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;


<urlset xmlns="">

	<?php foreach ($urls as $url): ?>


			<loc><?= $url ?></loc>


	<?php endforeach ?>


But xml doesn’t parse at the browser:

What I have to do to fix it?

Solved. I used

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

$headers = Yii::$app->response->headers;

$headers->add('Content-Type', 'text/xml');


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