无法在cakephp中编写XML网站地图 [英] unable to write xml sitemap in cakephp

查看:102
本文介绍了无法在cakephp中编写XML网站地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新为了使某些情况更清楚一些,我的页面没有显示文档树(这是我第一次看到此树)。我的xml内容只有一长行,没有任何xml元素/标记。

UPDATE To make it a little more clear to some about what's going on, my page does NOT display the document tree(the first time I've ever seen this). Just one long line of my xml content WITHOUT any xml elements/tags.

我关注了本教程,用于在cakephp中创建动态站点地图。本教程编写于2008年,并且我(继承了)正在Cakephp 1.3应用程序中工作(我假设本教程适用于1.3版本吗?我看过其他一些在2009年和2010年使用的代码相同的代码。 )除了令人痛苦的事实:我的查询中没有一个正在产生任何东西,当我导航到mysite.com/sitemap.xml时,我的输出是:

I followed this tutorial for creating a dynamic sitemap in cakephp. This tutorial was written in 2008 and I (have inherited) am working in a cakephp 1.3 app (I am assuming this tutorial would be valid for 1.3? I've looked at some others that pretty much used the same code from 2009 and 2010.) Apart from the distressing fact that NOT ONE of my queries is producing anything, my output when I navigate to mysite.com/sitemap.xml is this:

http://www.site.com/ daily 1.0 http://www.site.com/gulf-coast-finest/3 2012-10-01T19:00:00Z weekly 0.9 http://www.site.com/gulf-coast-finest/4 2012-10-01T19:00:00Z weekly 0.9

当我查看源代码时,我得到了正确格式的xml(但缺少关键数据),如下所示:

When I 'view source' I get correctly formatted xml (with the exception that I am missing crucial data), like below:

 <?xml version="1.0" encoding="UTF-8" ?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url> 
    <loc>http://www.site.com/</loc> 
    <changefreq>daily</changefreq> 
    <priority>1.0</priority> 
</url> 
 <!-- posts-->  

<url> 
    <loc>http://www.site.com/gulf-coast-finest/3</loc> 
    <lastmod>2012-10-01T19:00:00Z</lastmod> 
    <changefreq>weekly</changefreq>
    <priority>0.9</priority> 
</url>

<url> 
    <loc>http://www.site.com/gulf-coast-finest/4</loc> 
    <lastmod>2012-10-01T19:00:00Z</lastmod> 
    <changefreq>weekly</changefreq>
    <priority>0.9</priority> 
</url>
</urlset>  
.... 

我想知道为什么没有空格或xml标记,除非我查看源代码。这是我的路由文件中的相关代码:

I am tearing my hair out wondering why there is no whitespace or xml tags unless I view source. Here is the relevant code from my routes file:

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

我的应用程序/视图/布局/xml/default.ctp文件:

my app/views/layouts/xml/default.ctp file:

<?php
header("content-type: text/xml");
echo $this->Xml->header();
echo $content_for_layout;
?> 

我的应用程序/视图/站点地图/xml/index.ctp文件:

my app/views/sitemaps/xml/index.ctp file:

<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> 
 <!-- posts-->  
 <?php foreach ($static as $s):?> 
<url> 
    <loc><?php echo Router::url('/gulf-coast-finest/'.$s['Article']['Article.link_txt'].'/'.$s['Article']['id'],true); ?></loc> 
    <lastmod><?php echo $time->toAtom('2012-10-01 19:00:00'); ?></lastmod> 
    <changefreq>weekly</changefreq>
    <priority>0.9</priority> 
</url>
<?php endforeach; ?>
</urlset> 

和我显然已完成的混乱的控制器:

and my obviously completed messed up controller:

<?php 
class SitemapsController extends AppController{ 

var $name = 'Sitemaps'; 
var $uses = array('Category', 'Listing', 'Article', 'Location'); 
var $helpers = array('Time'); 
var $components = array('RequestHandler'); 
function beforeFilter() {
    //debug logs will destroy xml format, make sure were not in drbug mode 
Configure::write ('debug', 0); 

}
function index (){  
    $this->RequestHandler->respondAs('xml'); 
    $this->viewPath .= '/xml';
     $this->layoutPath = 'xml';
    $a=$this->Article->find('all', array('fields'=>array('Article.id', 'Article.link_txt')));
    $this->set('static', $a);


}
} 
?> 

非常重要的数据丢失问题可能适合另一个问题。 XML文件只有一行并且没有人类可见的标签,这不是我可以接受的事情。我想知道为什么会这样做

The very important missing data issue is probably suited to another question. The XML file being one line and having no tags visible to humans is not something I'm ok with. I want to know why it's doing this

推荐答案

我遇到了同样的问题,我想我已经找到了解决方案。我认为您在HTML / XML代码前面有空格

I've the same problem, and i think I've found the solution. I think you've space in front of your HTML / XML code

<-- space here--><?xml version="1.0" encoding="UTF-8" ?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

您可以尝试在您的sitemap_controller.php,如果您在PHP结束标记后有一个空格

you can try to find in your sitemap_controller.php if you've a space after PHP closing tag

?>__space here__

这篇关于无法在cakephp中编写XML网站地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆