Check that you are not closing PHP tags on any of your scripts, ie "?>" as this will cause anything following to be sent to the output.
Aug 7, 2015
8
Level 2
Blade file: Timestamp format and leading space
Hi! I'm building a Sitemap Generator and have some issues with it and kindly ask if you could give me some input.
- I want to change the date format of {{ unix() }} to match the standards of sitemap protocol. Is this in some way possible in a Blade file?
- You can see that I'm forwading a $tmp variable into the view to render this:
<?xml version='1.0' encoding='UTF-8'?>. I also tried to put this into the Blade file:<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>But how ever I do this he puts a leading space before the line. :-) How can I get rid of this? (This produces an error since it expulses against the Sitemap protocol.
Here is my code:
// Method for creating the view
public function showSitemap()
{
$pages = Page::all();
$tmp = "<?xml version='1.0' encoding='UTF-8'?>";
return response()->view('backend.pages.sitemap', ['pages' => $pages, 'tmp' => $tmp])->header('Content-Type', 'text/xml;charset=UTF-8');
}
// My Blade File
{!! $tmp !!}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
@foreach($pages as $page)
<url>
<loc>http://www.example.org/{{ $page->url }}</loc>
<lastmod>{{ unix() }}</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
@endforeach
</urlset>
And the error message:

Level 122
I have a working example. Not sure what it is that is different from my approach;
routes.php
Route::get('sitemap.xml', function() {
$pages = [ ['url' => 'abc'],
['url' => '123']
];
$xmlhdr = '<?xml version="1.0" encoding="UTF-8" ?>';
$view = view('sitemap', compact('xmlhdr','pages'));
return Response::make($view, '200')->header('Content-Type', 'text/xml');
});
and sitemap.blade.php
{!! $xmlhdr !!}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
@foreach($pages as $page)
<url>
<loc>http://www.example.org/{{ $page['url'] }}</loc>
<lastmod></lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
@endforeach
</urlset>
1 like
Please or to participate in this conversation.