2014-03-05 46 views
1

不工作我試圖自動生成的CakePHP 2.1一個網站地圖,我改變了頭響應爲text/xml,但瀏覽器得到一個text/html的響應。XML頭在CakePHP的2.1

SitemapsController:

<?php 
class SitemapsController extends AppController{ 

    var $uses = array('Post'); 
    var $helpers = array('Time'); 
    var $components = array('RequestHandler'); 

    function index(){ 

     Configure::write ('debug', 0); 

     $this->set('posts', $this->Post->find('all', array('conditions'=>array('publique'=>1)))); 

     $this->RequestHandler->respondAs('xml'); 

    } 

    public function beforeFilter() { 
     parent::beforeFilter(); 
     $this->Auth->allow(); 
    } 
    public function isAuthorized($user) { 
     return true; 
    } 
} 
?> 

/view/Sitemaps/xml/index.ctp

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> 
    <url> 
     <loc><?php echo Router::url('/',true); ?></loc> 
     <changefreq>daily</changefreq> 
     <priority>1.0</priority> 
    </url>  
    <?php foreach ($posts as $post):?> 
    <url> 
     <loc><?php echo Router::url(array('action'=>'view', 'id'=>$post['Post']['id'], 'slug'=>$post['Post']['slug']),true); ?></loc> 
     <lastmod><?php echo $this->Time->toAtom($post['Post']['created']); ?></lastmod> 
     <priority>0.8</priority> 
    </url> 
    <?php endforeach; ?> 
</urlset> 

佈局/ XML/default.thtml中

<?php header('Content-type: text/xml'); ?> 
<?= $this->fetch('content'); ?> 

routes.php文件

Router::parseExtensions('xml'); 
Router::connect('/sitemap',array('controller'=>'sitemaps','action'=>'index','url'=>array('ext'=>'xml'))); 

回答

0

你試過設置完整的內容類型的requestHandler?

$this->RequestHandler->respondAs('application/xml'); 

因爲這取決於您的關聯模型是必要的。或者嘗試通過選項數組:

RequestHandlerComponent::respondAs($type, $options)¶ 
Parameters: 
$type (string) – Friendly content type name ex. xml, rss or a full content type like application/x-shockwave 
$options (array) – If $type is a friendly type name that has more than one content association, $index is used to select the content type. 
+0

同樣的問題:(這似乎是內容類型不改變 –

+0

,並指定在控制器中的渲染:在respondAs後,添加一行爲$這個 - > RequestHandler-> renderAs($此,「XML」); – ylerjen

+0

的requestHandler不會改變任何東西!,我得到了相同的結果,有或沒有它! –

-1
$this->response->type('text/xml');