简体   繁体   English

使用laravel渲染站点地图时出错

[英]error rendering a sitemap with laravel

I have a problem rendering a sitemap with laravel. 我在使用laravel渲染站点地图时遇到问题。 Generated xml seems ok but when i try to call the url from chrome or firefox i got an error 生成的xml似乎还可以,但是当我尝试从chrome或firefox调用url时出现错误

 error on line 2 at column 6: XML declaration allowed only at the start of the document

In fact line 1 of the document is empty and xml declaration starts on line 2 实际上,文档的第1行为空,并且xml声明从第2行开始

Here is my code : 这是我的代码:

 return Response::view('sitemap.index', ['agences' => $agences])->header('Content-Type', 'application/xml');

i tried that syntax too : 我也尝试过这种语法:

 $xml = View::make('sitemap.index', ['agences' => $agences]);
 return Response::make($xml, 200)->header('Content-Type', 'application/xml');

That way i could do 这样我可以做

 dd($xml->render()); 

and realize the string returned has no empty first line. 并意识到返回的字符串没有空的第一行。 So i'm guessing Response::make is the one to blame but i really have no idea where to look from there 所以我猜是Response :: make是要怪的,但我真的不知道从哪里看

Ok I'm gonna post my own answer cause that was tricky and it costs me a day, the good thing is my knowledge of laravel has slightly increased. 好吧,我要发表我自己的答案,原因很棘手,而且花了我一天的时间,好消息是我对laravel的了解略有增加。

So i had my xml sitemap beginning with an empty line, and that created an error on browser. 所以我的xml站点地图以空行开头,这在浏览器上造成了错误。 Xml was first generated using a blade template. Xml首先使用刀片模板生成。 As it didn't work i decided to use RoumenDamianoff/laravel-sitemap 由于不起作用,我决定使用RoumenDamianoff / laravel-sitemap

But i had the same problem. 但是我有同样的问题。 So finally i decided to generate Xml myself again using SimpleXmlElement and it changes nothing. 所以最后我决定再次使用SimpleXmlElement自己生成Xml,并且它什么都没有改变。

At that point i begun to dig in Laravel internal's to see where that empty line could come from in the request lifecycle. 从那时起,我开始挖掘Laravel内部的,以查看该空行可能来自请求生命周期。

Basically my sitemap is very simple : 基本上我的站点地图非常简单:

$urlset = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" /><!--?xml version="1.0" encoding="UTF-8"?-->');
datas = MyModel::All();
foreach($datas as $index=>$data){
// generate sitemap
}
$dom = new DomDocument();
$dom->loadXML($urlset->asXML());
$dom->formatOutput = true;
//output xml
$xml = $dom->saveXML();
$response = Response::make($xml, 200, ['Content-Type' => 'application/xml']);

Just to test i decided to change the model i was requesting, and then my xml generated without that first empty line. 为了测试,我决定更改我要的模型,然后生成的xml没有第一个空行。 So i decided to investigate the model itself and find the error. 因此,我决定调查模型本身并找到错误。 The model file just had an empty line before php opening tag. 在php开头标记之前,模型文件只有一个空行。

Deleting that empty line has solved my problem .... 删除空行解决了我的问题....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM