2014-03-05 4 views
1

CakePHP 2.1에서 자동으로 Sitemap을 생성하려고하는데 헤더 응답을 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.ctp

<?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에 전체 content-type을 설정하려고 시도 했습니까?

$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 후, 줄을 추가 $ this-> RequestHandler-> renderAs ($ this, 'xml'); – ylerjen

+0

requestHandler는 아무 것도 변경하지 않는다 !!, 나는 같은 결과를 얻었거나 만들지 않았다 !! –

-1
$this->response->type('text/xml'); 
관련 문제