前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PHP生成网站Sitemap,包含默认、分类、文章、标签

PHP生成网站Sitemap,包含默认、分类、文章、标签

作者头像
Kunkka Wu
发布2022-01-13 11:05:00
4410
发布2022-01-13 11:05:00
举报
文章被收录于专栏:算法协议

PHP生成网站Sitemap,包含默认、分类、文章、标签、profile

代码语言:javascript
复制
<?php

namespace App\Libs;

use App\Services\ArticleService;
use App\Services\CategoryService;
use App\Services\TagService;
use App\Services\UserService;

class Sitemap {

    public static $url = 'http://blog.getcoder.cn';
    public static $defaultXml = [
        [
            'loc' => '/',
            'priority' => 1.00,
        ],
        [
            'loc' => '/hot',
            'priority' => 0.80,
        ],
        [
            'loc' => '/login',
            'priority' => 0.80,
        ],
    ];

    public static function createSitemap(){
        // 创建一个DOMDocument对象
        $dom = new \DOMDocument("1.0","utf-8");
        header("Content-Type: text/xml");
        // 创建根节点
        $root = $dom->createElement("urlset");
        $dom->appendChild($root);
        //生成默认的Url
        self::createUrl($root, $dom, self::$defaultXml);

        //生成分类
        $catList = CategoryService::getList();
        $catXml = [];
        if(!empty($catList)){
            foreach ($catList as $item){
                $catXml[] = [
                    'loc' => '/category?id=' . $item->id,
                    'priority' => 0.64,
                    'lastmod' => $item->update_time
                ];
            }
        }
        self::createUrl($root, $dom, $catXml);

        //生成文章
        $articleList = ArticleService::getList();
        $articleXml = [];
        if(!empty($articleList)){
            foreach ($articleList as $item){
                $articleXml[] = [
                    'loc' => '/article?id=' . $item->id,
                    'priority' => 0.94,
                    'lastmod' => $item->update_time
                ];
            }
        }
        self::createUrl($root, $dom, $articleXml);

        //生成Tag
        $tagList = TagService::getList();
        $tagXml = [];
        if(!empty($tagList)){
            foreach ($tagList as $item){
                $tagXml[] = [
                    'loc' => '/tag?id=' . $item->id,
                    'priority' => 0.64,
                    'lastmod' => $item->create_time
                ];
            }
        }
        self::createUrl($root, $dom, $tagXml);

        //生成用户主页
        $userList = UserService::getList();
        $userXml = [];
        if(!empty($userList)){
            foreach ($userList as $item){
                $userXml[] = [
                    'loc' => '/profile?uid=' . $item->uid,
                    'priority' => 0.7,
                    'lastmod' => $item->create_time
                ];
            }
        }
        self::createUrl($root, $dom, $userXml);

        //生成xml文件
        $dom->save(public_path() . "/sitemap.xml");exit;
    }

    public static function createUrl(&$root, &$dom, $data){
        $date = date('Y-m-d');
        foreach($data as $value){
            // 建立根下子节点track
            $track = $dom->createElement("url");
            $root->appendChild($track);
            // 建立track节点下元素
            $loc = $dom->createElement("loc");
            $priority = $dom->createElement("priority");
            $lastmod = $dom->createElement("lastmod");
            $changefreq = $dom->createElement("changefreq");

            $track->appendChild($loc);
            $track->appendChild($priority);
            $track->appendChild($lastmod);
            $track->appendChild($changefreq);

            // 赋值
            $locNode        = $dom->createTextNode(self::$url.$value['loc']);
            $date = empty($value['lastmod']) ? $date : date('Y-m-d',  strtotime($value['lastmod']));
            $lastmodNode    = $dom->createTextNode($date);
            $changefreqNode = $dom->createTextNode('daily');
            $priorityNode   = $dom->createTextNode($value['priority']);
            $loc->appendChild($locNode);
            $lastmod->appendChild($lastmodNode);
            $changefreq->appendChild($changefreqNode);
            $priority->appendChild($priorityNode);
        }
    }

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/01/13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档