2014-03-05 26 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');