Create XML feeds with Laravel Blade

Erik Masny
2 min readMar 19, 2024
Title image for XML in Laravel Blade

In modern web applications, XML (eXtensible Markup Language) is widely used for communication and transporting data. Whether it was sending and consuming data via APIs, creating product feeds for ecommerce websites, many developers have dealt with creating XML at some point. In this article, we will take a look at super-easy way for creating XML in PHP/Laravel, with the help of Blade.

Different ways to generate XML in Laravel

There are multiple ways to tackle XML generation. We have built-in PHP extension XMLWriter, which does the job. But let’s be honest, it is a little bit clumsy (example usage can be found here). There are also many third party packages. Some using OOP way of generating the XML, other taking a different approach, for example creating XML from array (eg. Spatie’s array-to-xml package). The approach we will discuss in this article, is using Laravel Blade, the same way you generate your HTML.

Why would I want to use Blade for XML?

The main reason is probably the readability and how straightforward this approach is. You don’t have to install any packages and you work with the XML directly, with the superpower of Blade (in contrast with working with an abstract array translated to XML for example).

Laravel Blade is mainly used for HTML, but you are not limited from using it for other things as well. Blade, as a templating engine, will translate your code written in Blade and PHP into string output. As you will see in the example, we can use it in a very elegant way to generate XML.

Example of creating XML with Laravel Blade

Let’s use a simple product feed, that will be available at /product-feed.xml, as an example. This is the structure that we want to generate:

<?xml version="1.0" encoding="utf-8"?>
<products>
<product>
<title>Lord of the rings book</title>
<price>10.99</price>
<stock>5</stock>
</product>
<product>
<title>Hobbit book</title>
<price>9.99</price>
<stock>3</stock>
</product>
</products>

The process is straightforward. All we have to do, is to add a route in routes/web.php, where we render the Blade view, and return it’s content:

Route::get('product-feed.xml', function () {
$xml = view('feed.product', ['products' => \App\Models\Product::all()])->render();

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

The content of the resources/views/feed/product.blade.php would be following:

<?xml version="1.0" encoding="utf-8"?>
<products>
@foreach($products as $product)
<product>
<title>{{ $product->name }}</title>
<price>{{ $product->price }}</price>
<stock>{{ $product->stock }}</stock>
</product>
@endforeach
</products>

And that’s it. We just created a working product feed with just a few lines, with Blade which you are probably familiar with. You can see the example GitHub repo.

What do you think, will you use this approach in the future? Let me know your opinion in the comments.

--

--