使用 Codeigniter 生成站点地图 [英] Sitemap generation with Codeigniter

查看:25
本文介绍了使用 Codeigniter 生成站点地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 Codeigniter 应用程序中生成站点地图.我找到了一些库,但它们都已经过时并且有错误.

I need to generate a sitemap in a Codeigniter application. I found a few libraries but all of them are outdated and have bug.

我真的需要一个单独的库吗?

我想知道在 Codeigniter 中生成站点地图的最佳方法.

I want to know the best way to generate the sitemap in Codeigniter.

推荐答案

你可以使用我的代码:

控制器/seo.php

controllers/seo.php

Class Seo extends CI_Controller {

    function sitemap()
    {

        $data = "";//select urls from DB to Array
        header("Content-Type: text/xml;charset=iso-8859-1");
        $this->load->view("sitemap",$data);
    }
}

views/sitemap.php

views/sitemap.php

<?= '<?xml version="1.0" encoding="UTF-8" ?>' ?>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc><?= base_url();?></loc> 
        <priority>1.0</priority>
    </url>

    <!-- My code is looking quite different, but the principle is similar -->
    <?php foreach($data as $url) { ?>
    <url>
        <loc><?= base_url().$url ?></loc>
        <priority>0.5</priority>
    </url>
    <?php } ?>

</urlset>

在 config/routes.php 中添加一行

add line to config/routes.php

$route['seo/sitemap.xml'] = "seo/sitemap";

对不起,如果代码中有一些错误,我特地为您制作的.如果有错误,您可以通过了解原理轻松修复它们.

Sorry if there are some errors in the code, I made it especially for you. If there are errors, you can fix them easily by understanding the principle.

这篇关于使用 Codeigniter 生成站点地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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