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

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 = &#39;http://blog.getcoder.cn&#39;;
public static $defaultXml = [
    [
        &#39;loc&#39; =&gt; &#39;/&#39;,
        &#39;priority&#39; =&gt; 1.00,
    ],
    [
        &#39;loc&#39; =&gt; &#39;/hot&#39;,
        &#39;priority&#39; =&gt; 0.80,
    ],
    [
        &#39;loc&#39; =&gt; &#39;/login&#39;,
        &#39;priority&#39; =&gt; 0.80,
    ],
];

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

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

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

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

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

    //生成xml文件
    $dom-&gt;save(public_path() . &#34;/sitemap.xml&#34;);exit;
}

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

        $track-&gt;appendChild($loc);
        $track-&gt;appendChild($priority);
        $track-&gt;appendChild($lastmod);
        $track-&gt;appendChild($changefreq);

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

}