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.

Controller:




public function actionIndex()

	{

		$urls = [];


                // ....


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


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

	}



View:




<?php

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

/* @var $urls */

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

?>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">


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

		<url>

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

		</url>

	<?php endforeach ?>


</urlset>



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');



instead


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