From 3ab345f41c1d23cc48655f89fec50e9097ae28e1 Mon Sep 17 00:00:00 2001 From: augushong Date: Wed, 16 Mar 2022 10:40:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Esietmap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/tools/Sitemap.php | 55 ++++++++++++++++++++++++++++++++++++ composer.json | 3 +- route/app.php | 10 ++++++- 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 app/common/tools/Sitemap.php diff --git a/app/common/tools/Sitemap.php b/app/common/tools/Sitemap.php new file mode 100644 index 0000000..1e95d29 --- /dev/null +++ b/app/common/tools/Sitemap.php @@ -0,0 +1,55 @@ +setChangeFreq('always'); + + $index_url->setPriority(0.9); + + $urlset_page_post->add($index_url); + + $list_post = Post::cache(60)->where('status',1)->select(); + + foreach ($list_post as $model_post){ + $url_post = new Url($model_post->read_url); + $url_post->setChangeFreq('yearly'); + + $last_mod_date = new DateTime($model_post->publish_time_datetime); + + $url_post->setLastMod($last_mod_date); + + $url_post->setPriority(0.9); + + $urlset_page_post->add($url_post); + + } + + + $dirver = new XmlWriterDriver(); + // $urlset_page_list->accept($dirver); + + $urlset_page_post->accept($dirver); + + return $dirver->output(); + + + } +} diff --git a/composer.json b/composer.json index c141589..6a3c0f6 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,8 @@ "topthink/think-helper": "^3.1", "topthink/think-captcha": "^3.0", "topthink/think-multi-app": "^1.0", - "ulthon/user_hub_client": "^1.0" + "ulthon/user_hub_client": "^1.0", + "thepixeldeveloper/sitemap": "^5.1" }, "require-dev": { "symfony/var-dumper": "^4.2" diff --git a/route/app.php b/route/app.php index 6b17cdc..4797fab 100644 --- a/route/app.php +++ b/route/app.php @@ -8,4 +8,12 @@ // +---------------------------------------------------------------------- // | Author: liu21st // +---------------------------------------------------------------------- -use think\facade\Route; \ No newline at end of file + +use app\common\tools\Sitemap; +use think\facade\Route; + +Route::rule('/site.xml', function () { + $content = Sitemap::init(); + + return xml($content); +});