I'm generating a Google products feed using Laravel 4. I'm using a Blade file to do this, but Laravel is adding a mystery space at the very start of the output, which causes Google to reject the XML file as invalid. Any ideas why?
My controller is:
public function googleFeed()
{
//generates a Google Merchant XML feed of products
$products = DB::table('products')->get();
$content = View::make('shop.googlefeed', ['products' => $products]);
return Response::make($content, '200')->header('Content-Type', 'text/xml');
}
And my blade file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<title>Title</title>
<link>http://example</link>
<description>Desc</description>
@foreach ($products as $product)
<item>
<g:id>{{($product->id)}}</g:id>
<title>{{($product->productname)}}</title>
<link>http://example.com/product/{{($product->slug)}}</link>
<description>{{($product->shortdesc)}}</description>
<g:image_link>{{($product->imgurlthumb)}}</g:image_link>
<g:price>{{($product->productprice)}} GBP</g:price>
<g:gtin>{{($product->gtin)}}</g:gtin>
<g:condition>new</g:condition>
<g:availability>in stock</g:availability>
</item>
@endforeach
</channel>
</rss>
No space in the Blade file. But, the output is:
<?xml version="1.0" encoding="ISO-8859-1"?>
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<title>Title</title>
....
Why is this space added? I just can't see where it would come from! It doesn't seem like much of a problem, but it causes the feed to fail validation and is rejected by Google. Failing being able to establish the source of it, is there a way to strip it out after processing?
Many thanks.
I solved this when I noticed that there was a space at the start of my routes.php file. Removing this fixed the issue. Simple but frustrating, hope it helps someone else! (The space could be at the start of most any file.)
<?php
Route::get('/', function () {
return "Hello";
});
...
To:
<?php
Route::get('/', function () {
return "Hello";
});
...