如何为CakePHP创建Sitemap? [英] How to create a Sitemap for CakePHP?

查看:107
本文介绍了如何为CakePHP创建Sitemap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个站点地图,但我对Sitemaps的使用情况知之甚少。我使用CakePHP。在google和指南上有很多软件,但仍然需要问一个简单的方法来创建CakePHP的Sitemap。

I want to create a sitemap, but I know very little about the usage of Sitemaps. I use CakePHP. There is a lot software on google and guides, but I still want ask anyway, for an easy way to create sitemaps for CakePHP.

我在服务器上上传了网站,它不依赖localhost。

I uploaded the website on the server, it doesn't rely on localhost.

推荐答案

这里有一个快速的例子,需求:

Here's a quick'n'dirty example for you to play with and adjust to your needs:

在您的控制器中:

public $components = array('RequestHandler');

public function sitemap()
{
    Configure::write('debug', 0);

    $articles = $this->Article->getSitemapInformation();

    $this->set(compact('articles'));
    $this->RequestHandler->respondAs('xml');
}

您的文章模型:

public function getSitemapInformation()
{
    return $this->find('all', array(/* your query here */));
}

查看:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <?php foreach ($articles as $article): ?>
    <url>
        <loc><?php echo Router::url(/* generate the URLs here */); ?></loc>
        <lastmod><?php echo $time->toAtom(/* last update time here */); ?></lastmod>
        <changefreq>weekly</changefreq>
    </url>
    <?php endforeach; ?>
</urlset>

这篇关于如何为CakePHP创建Sitemap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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