1.生成如下的xml文件与laobingcms生成的xml文件是一样的(具体实现方法在下面) :
<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>http://www.demo7.com/f7ae3/1.html</loc> <priority>0.6</priority> <lastmod>2020-09-28</lastmod> <changefreq>weekly</changefreq> </url> <url> <loc>http://www.demo7.com/f7ae3/2.html</loc> <priority>0.6</priority> <lastmod>2017-07-21</lastmod> <changefreq>weekly</changefreq> </url> <url> <loc>http://www.demo7.com/f7ae3/3.html</loc> <priority>0.6</priority> <lastmod>2020-09-28</lastmod> <changefreq>weekly</changefreq> </url> <url> <loc>http://www.demo7.com/f7ae3/4.html</loc> <priority>0.6</priority> <lastmod>2020-09-28</lastmod> <changefreq>weekly</changefreq> </url> <url> <loc>http://www.demo7.com/f7ae3/5.html</loc> <priority>0.6</priority> <lastmod>2017-07-21</lastmod> <changefreq>weekly</changefreq> </url> <url> <loc>http://www.demo7.com/f7ae3/6.html</loc> <priority>0.6</priority> <lastmod>2017-07-21</lastmod> <changefreq>weekly</changefreq> </url> </urlset>
2.具体实现的方法
文章的xml生成:
protected function articlexml() { $list = db('article') ->alias('a') ->join('column b','b.c_id=a.a_columnid') ->order('a_id asc')->select(); $sitemap = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\r\n"; foreach($list as $k=>$v){ $sitemap .= "<url>\r\n"."<loc>http://www.demo7.com/".$v['c_path'].'/'.$v['a_id'].'.html'."</loc>\r\n"."<priority>0.6</priority>\r\n<lastmod>".date('Y-m-d',$v['a_time'])."</lastmod>\r\n<changefreq>weekly</changefreq>\r\n</url>\r\n"; } $file = fopen("sitemap.xml","w"); fwrite($file,$sitemap); fclose($file); }
栏目的xml文件生成:
protected function columns() { $list = db('column')->order('c_id asc')->select(); $sitemap=''; foreach($list as $k=>$v){ $sitemap .= "<url>\r\n"."<loc>http://www.demo7.com/".$v['c_path']."</loc>\r\n"."<priority>0.6</priority>\r\n<lastmod>".date('Y-m-d',$v['c_time'])."</lastmod>\r\n<changefreq>weekly</changefreq>\r\n</url>\r\n"; } $sitemap .= '</urlset>'; file_put_contents('sitemap.xml',$sitemap,FILE_APPEND); }
在显示的首页控制器里面分别调用这两个方法,或者单独写个方法调用