Answer the question
In order to leave comments, you need to log in
How to save XML file using Yii2?
Hello. I am creating a sitemap in the admin panel and to save the xml file in the controller I wrote:
\Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
\Yii::$app->response->headers->add('Content-Type', 'text/xml');
$xml_sitemap = $this->renderPartial('sitemap', array(
'objects' => $objects,
));
$filepath = \Yii::$app->basePath . '/views/sitemap/sitemap.xml';
file_put_contents($filepath ,$xml_sitemap);
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php foreach($objects as $obj) { ?>
<url>
<loc><?= Yii::$app->params['siteUrl'] . '/' . $obj['url']; ?></loc>
<priority>1</priority>
</url>
<?php } ?>
</urlset>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question