Search code examples
laravellaravel-5.3laravel-blade

Blade View not rendering xml


I am trying to create an XML file in Laravel. I am passing data to a view.

However, the XML is not rendering.

This is what I am doing in my view

<?php header('Content-Type: text/xml'); ?>
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

@foreach ($merchants as $merchant)
<url>
<loc>{{ $merchant->merchant_url_text }}</loc>
</url>
@endforeach

</urlset>

This however is just printing the variable $merchant->merchant_url_text multiple times without any structure.

Any help with what I am doing wrong.


Solution

  • You're not sending the headers correctly, which is why your response is being interpreted as text/html. In Laravel, you may use the header method to add a series of headers to the response before sending it back to the user. Here's how:

    return response($content)
            ->withHeaders([
                'Content-Type' => 'text/xml'
            ]);