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')));
同樣的問題:(這似乎是內容類型不改變 –
,並指定在控制器中的渲染:在respondAs後,添加一行爲$這個 - > RequestHandler-> renderAs($此,「XML」); – ylerjen
的requestHandler不會改變任何東西!,我得到了相同的結果,有或沒有它! –