2016-11-15 55 views
0

IM解析XML試圖生成一個XML,但即時得到一個錯誤:FatalErrorException in 6955f07d83b93bb8aa89577b116866e228e0c155.php line 1 syntax error, unexpected 'version' (T_STRING).在laravel

我無法弄清楚什麼即時做錯了我的代碼。

我的控制器功能是:

public function feed($gallery_id){ 
     $products = Products::select("id","title","brand","url","path")->where("gallery_id",$gallery_id)->get(); 

     foreach ($products as $product) { 
      $product->path = url($product->path); 
     } 
     return response()->view('xml.gallery', compact('products'))->header('Content-Type', 'text/xml'); 

    } 

我的刀片(xml.gallery):

<?xml version="1.0"?> 
    <rss xmlns:g="http://base.google.com/ns/1.0" version="2.0"> 
     <channel> 
     <title>Test Store</title> 
     <link>http://domain.com</link> 
     <description>An example item from the feed</description> 
     @​foreach($products as $product) 
      <item> 
      <g:id>1</g:id> 
      <g:title>something</g:title> 
      <g:description>Solid plastic Dog Bowl in marine blue color</g:description> 
      <g:link>http://www.zara.com</g:link> 
      <g:image 

_link>http://domain.com/images/photos_gallery/14788772681.png</g:image_link> 
     <g:price>12 GBP</g:price> 
     <g:brand>Nike</g:brand> 
     <g:availability>in stock</g:availability> 
     <g:condition>new</g:condition> 
    </item> 
    @​endforeach 
    </channel> 
</rss> 

回答

2

我們圍繞它得到的方式是XML頭存儲爲一個變量,然後其傳遞:

$xml_version = '<?xml version="1.0"?>'; 

return response()->view('xml.gallery', compact('products', 'xml_version'))->header('Content-Type', 'text/xml'); 

然後您可以放入您的刀片:

{{$xml_version}} 
2

看起來您的short_open_tag已啓用。它告訴PHP是否應該允許PHP的開放標籤的簡寫形式(<? ?>)。如果您想將PHP與XML結合使用,則可以禁用此選項,以便內嵌使用<?xml ?>

但其他簡單的解決方案將是寫在你的視圖文件下面的代碼:

<?php echo '<?xml version="1.0"?>'; ?>