2017-01-04 44 views
2
由于要添加到渲染文件的开头XML在第一行和空行应该发生的声明

这导致此错误Symfony的嫩枝和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; 
    } 
} 

使用此代码时,我总是得到错误:

错误

enter image description here

回答

3

试试这个:

$response = $this->render("MarketplaceBundle:sitemap:sitemap.xml.twig", array(
     'urls' => $urls 
    )); 
    $response->headers->add(array('Content-Type' => 'application/xml')); 
    return $response; 

$this->render()已经返回响应,所以你不需要它嵌入到另一个Response对象。

+0

由于一些愚蠢的原因,它没有工作之前,它现在谢谢。 –