A
A
Alexander Stepanov2019-10-11 15:25:34
Yii
Alexander Stepanov, 2019-10-11 15:25:34

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,
        ]);
    }

Conscious view:
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>

It seems like everything is fine and opens, but the feed goes between the response tags in one line, and in the source code all '<' and '>' are displayed as '&lt;' и '&gt;'
Maybe the GoogleMerchant robot is hiding, but somehow it's not beautiful ... and it's not clear - why not a regular structure, but a stupid string

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Stepanov, 2019-10-16
@Exebeche

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 question

Ask a Question

731 491 924 answers to any question