本文是 基于thinkphp3.1的 但是道理都是相通的!下面我吧代码贴出来供大家参考!
<?php class SitemapAction extends BackendAction { public function _initialize() { parent::_initialize(); $this->_mod = D('menu'); } public function index() { //$tables = M()->db()->getTables();//获取网站所有的表 $site_url = M('setting')->where(array('name'=>"site_url"))->limit(1)->find(); $site_url = $site_url['data']; $str = '<?xml version="1.0" encoding="utf-8"?>'.PHP_EOL; $str .= '<urlset>'.PHP_EOL; // article 表 $articles = M('article')->field('id,add_time,last_time')->select(); foreach ($articles as $k => $v) { $str .= '<loc>'.$site_url.'index.php?m=index&a=read&id='.$v['id'].'</loc>'.PHP_EOL; if($v['last_time']){ $str .= '<lastmod>'.date('Y-m-d' ,$v['last_time']).'</lastmod>'.PHP_EOL; }else{ $str .= '<lastmod>'.date('Y-m-d' ,$v['add_time']).'</lastmod>'.PHP_EOL; } $str .= '<priority>1.0</priority >'.PHP_EOL; $str .= '<changefreq> always </changefreq>'.PHP_EOL.PHP_EOL.PHP_EOL; } // nav表 $nav = M('nav')->select(); foreach ($nav as $k => $v) { $str .= '<loc>'.$site_url.'index.php?m=index&a=cate&id='.$v['id'].'</loc>'.PHP_EOL; $str .= '<lastmod>'.date('Y-m-d' ,time()).'</lastmod>'.PHP_EOL; $str .= '<priority>1.0</priority >'.PHP_EOL; $str .= '<changefreq> always </changefreq>'.PHP_EOL.PHP_EOL.PHP_EOL; } // article_cate表 $article_cate = M('article_cate')->select(); foreach ($article_cate as $k => $v) { $str .= '<loc>'.$site_url.'index.php?m=index&a=cate&id='.$v['id'].'</loc>'.PHP_EOL; $str .= '<lastmod>'.date('Y-m-d' ,time()).'</lastmod>'.PHP_EOL; $str .= '<priority>1.0</priority >'.PHP_EOL; $str .= '<changefreq> always </changefreq>'.PHP_EOL.PHP_EOL.PHP_EOL; } $str .='</urlset>'.PHP_EOL; file_put_contents(APP_PATH."/../Sitemap.xml", $str); echo '已经更新成功!可以到网站根目录下的Sitemap.xml查看!'; echo '<script>alert("已经更新成功!可以到网站根目录下的Sitemap.xml查看!");</script>'; } }
暴力方法,喜欢可以自己拿去并修改成自己想要的咯!感谢大家