Answer the question
In order to leave comments, you need to log in
Why xml is generated in one line?
Created action:
public function actionFeed()
{
$products = Product::find()->all();
\Yii::$app->response->format = Response::FORMAT_XML;
\Yii::$app->response->headers->add('Content-Type', 'text/xml');
return $this->renderPartial('merchant', [
'products' => $products,
]);
}
use yii\helpers\Html;
use yii\helpers\Url;
?>
<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/css" href="/css/doc.css"?>
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">
<channel>
<title>Manufacture17</title>
<description>Интернет-магазин тканей</description>
<link>https://manufacture17.com.ua</link>
<?php foreach ($products as $product): ?>
<item>
<link><?= Html::encode(Url::to(['slug-product', 'slug' => $product->slug])) ?></link>
<g:id><?= $product->code ?></g:id>
<g:price><?= $product->price_new ?> UAH</g:price>
<g:condition>new</g:condition>
<g:availability>in_stock</g:availability>
<g:product_type><?= $product->category->name ?>></g:product_type>
<g:image_link><?= $product->mainPhoto->getThumbFileUrl('file', 'large') ?></g:image_link>
<title><?= $product->name ?></title>
<description><?= $product->description ?></description>
</item>
<?php endforeach; ?>
</channel>
</rss>
'<' и '>'
Answer the question
In order to leave comments, you need to log in
No matter how hard I tried, nothing worked with Responce ..
The best thing that happened was displaying the entire feed into a string, and at the end - an error notification.
As the log showed, the xml header was duplicated. Strangely, it was duplicated, even when I removed the "mentions" of this title from both the model and the controller.
The task was solved thanks to XMLWriter in the model and sendContentAsFile in the controller - the page is displayed without errors and the way the XML file should look like.
Z.Y.
I was very impressed with the ability of XMLWriter to set a bunch of attributes, as well as build any nesting of objects))
All the beaver
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question