thinkcmf sitemap生成


<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\HomeBaseController;
use app\portal\model\PortalCategoryModel;
use app\portal\model\PortalPostModel;
use think\Db;

class SitemapController extends HomeBaseController
{
   public function index()
   {
       $site = $this->siteinfo();
       $url = $site."/\r\n";
       $html = '<a href="'.$site.'/">首页</a>';
       $xml = '<?xml version="1.0" encoding="utf-8"?><urlset><url><loc>'.$site.'/</loc></url>';
       $portalCategoryModel = new PortalCategoryModel();
       $category = $portalCategoryModel->where('status', 1)->where('delete_time', 0)->select();
       foreach($category as $val)
       {
           $geturl = $this->geturl('portal/List/index?id='.$val['id'].'');
           $url .= $site."/".$geturl['url'].".html\r\n";
           $html .= '<a href="'.$site.'/'.$geturl['url'].'.html">'.$val['name'].'</a>'."<br>";
           $xml .= '<url><loc>'.$site.'/'.$geturl['url'].'.html</loc></url>';
       }
       $PortalPostModel = new PortalPostModel();

       $where['post_status'] = '1';
       $where['post_type'] = '2';
       $article = $PortalPostModel->where($where)->where('delete_time', 0)->select();
       foreach($article as $value)
       {
           $geturl = $this->geturl('portal/Page/index?id='.$value['id'].'');
           if(empty($geturl['url']))
           {
               $url .= $site."/portal/Page/index?id=".$value['id']."\r\n";
               $html .=  '<a href="'.$site.'/portal/Page/index?id='.$value['id'].'">'.$value['post_title'].'</a>'."<br>";
               $xml .= '<url><loc>'.$site.'/portal/Page/index?id='.$value['id'].'</loc></url>';
           }
           else
           {
               $url .= $site."/".$geturl['url'].".html"."\r\n";
               $html .=  '<a href="'.$site.'/'.$geturl['url'].'.html">'.$value['post_title'].'</a>'."<br>";
               $xml .= '<url><loc>'.$site.'/'.$geturl['url'].'.html</loc></url>';
           }
       }
       $where['post_status'] = '1';
       $where['post_type'] = '1';
       $article = $PortalPostModel->where($where)->where('delete_time', 0)->select();
       foreach($article as $value)
       {
           $url .= $site."/article/".$value['id'].".html"."\r\n";
           $html .=  '<a href="'.$site.'/article/'.$value['id'].'.html">'.$value['post_title'].'</a>'."<br>";
           $xml .= '<url><loc>'.$site.'/article/'.$value['id'].'.html</loc></url>';
       }
       $xml .= '</urlset>';
       $this->updatetxt($url);
       $this->updatexml($xml);
   }
   private function geturl($url)
   {
       $result = Db::name('route')->where(["full_url"=>$url])->find();
       return $result;
   }
   private function siteinfo()
   {
       $result = Db::name('option')->find();
       $resultarr = json_decode($result['option_value'],true);
       return $resultarr['site_url'];
   }
   private function updatetxt($data)
   {
       $this->writefile('txt',$data);
   }
   private function updatexml($data)
   {
       $this->writefile('xml',$data);
   }
   private function writefile($type,$data)
   {
       $myfile = fopen($_SERVER['DOCUMENT_ROOT']."/site.".$type, "w") or die("Unable to open file!");
       fwrite($myfile, $data);
       fclose($myfile);
   }
}


打赏

评论

暂时关闭,稍后恢复~
文档请看10遍以上!有问题可加QQ群!
发表话题

源素材推荐

提问必知

0.话题发布后没及时显示,请联系官方QQ群管理;
1.话题支持代码,qq截屏直接粘贴,和QQ群一样;
2.问题描述清,比如服务器版本,程序版本;
3.能上代码,就不用文字;
4.把问题发到话题后再发QQ群;
5.如果感觉回复者解答的不错,可以发红包!
6.多帮助别人也是对自己的锻炼!
7.付费讨论中只有提问者和付费用户才可以查看回复

积分规则

1.评论积分+1;每天最多10次奖励
2.发布话题积分+1;每天最多5次奖励
3.垃圾评论积分-500;
4.黄色,暴力,违反我国法律评论直接封号


ThinkCMF 8.0.0发布啦!祝大家节日快乐! 立即体验!