문제

I need to automatic generate sitemaps by using CakePHP. I followed this tutorial: http://designaeon.com/blog/2012/07/cakephp-xml-sitemap-generation/ step by step, but my http://mysite123.com/sitemap.xml only shows me following error:

This page contains the following errors:

error on line 2 at column 6: XML declaration allowed only at the start of the document Below is a rendering of the page up to the first error.

My code looks like this:

Controller/SitemapsController.php:

 <?php
        class SitemapsController extends AppController{ 
            var $name = 'Sitemaps';
            var $uses = array('Video');

            function index(){   
                $this->set('videos', $this->Video->find('all'));
                Configure::write ('debug', 0);
            }

       } 
    ?>

View/Sitemaps/xml/index.ctp:

  <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
        <?php foreach ($videos as $vid): ?>
        <url>
            <loc><?php echo Router::url('/',true); ?>post/
                <?php echo $vid['Video']['id'];?></loc>
            <changefreq>daily</changefreq>
            <priority>1.0</priority>
        </url>
        <?php endforeach; ?>
    </urlset>

View/Layouts/xml/default.ctp:

<?php header('Content-type: text/xml'); ?> 
<?php echo $content_for_layout; ?> 

routes.php:

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

Could you please help me?

도움이 되었습니까?

해결책

I would recommend putting this in your Controller's action instead of setting the header value using PHP in your layout:

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

It's the real 'Cake' way of doing it, and should solve your problem (which is probably whitespace or something being output before the header).

Of course, you need to be using the RequestHandler component, so put this in the controller if you don't already have it:

var $components = array('RequestHandler'); 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top