因为xml的声明应该在第一行发生,并且空行被添加到渲染文件的开头,导致此错误
所以问题是如何从文档的开头删除空行?或者是否有其他方式 - 而不是捆绑 - 使用站点地图?
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="{{asset("sitemap.xsl")}}"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{% for url in urls %}
<url>{# check if hostname is not alreay in url#}
<loc>{{url.loc}}</loc>
</url>
{% endfor %}
</urlset>
<?php
namespace MarketplaceBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
// use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class SitemapController extends Controller
{
/**
* @Route("/sitemap.{_format}", name="marketplace_sitemap", Requirements={"_format" = "xml"})
*/
public function sitemapAction()
{
$urls = array();
// add some urls homepage
$urls[] = array('loc' => $this->get('router')->generate('marketplace'), 'changefreq' => 'weekly', 'priority' => '1.0');
// service
$response = new Response(
$this->render("MarketplaceBundle:sitemap:sitemap.xml.twig",
array('urls' => $urls) ),
200,
array('Content-Type' => 'application/xml')
);
return $response;
}
}
使用此代码时,我总是收到错误:
答案 0 :(得分:3)
试试这个:
$this->render()
Response
已经返回了一个回复,因此您无需将其嵌入另一个{{1}}对象中。