[英]How to render a view as XML in Laravel 5?
sitemap.index看起來像這樣:
<?php
use Vanilo\Product\Models\Product;
header('Content-Type: text/xml');
?>
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php Product::all()->each(function($product) {
echo "<url><loc>";
echo route('product.show', $product);
echo "</loc><lastmod>";
echo \Carbon\Carbon::now();
echo "</lastmod><changefreq>monthly</changefreq><priority>0.8</priority$
}); ?>
</urlset>
web.php看起來像這樣,我的路線是:
Route::get('/sitemap', function() {
return view('sitemap.index')->withHeaders([
'Content-Type' => 'text/xml'
]);
});
但是,我的頁面如下所示:
https://evosmedia.uk/shop/p/blk-med-tshirt2019-07-21 08:33:54monthly0.8https://evosmedia.uk/shop/p/small-black-tshirt2019-07-21 08:33:54monthly0.8
如何將路線呈現為XML?
我設法通過使用Response::make()
並提供View::make()
Route::get('/sitemap', function() {
$contents = View::make('sitemap.index');
$response = Response::make($contents, 200);
$response->header('Content-Type', 'text/xml');
return $response;
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.