From f74d500055abbb12349fe5a3321bf260e0a91cef Mon Sep 17 00:00:00 2001 From: yumo Date: Mon, 24 Jul 2023 14:43:08 +0800 Subject: [PATCH] =?UTF-8?q?sitemap=E7=9A=84=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/Index.php | 84 ---------------------------------- app/index/controller/Index.php | 77 +++++++++++++++++++++++++++++++ app/index/route/app.php | 18 ++++++++ public/doc/路由.md | 7 +++ route/app.php | 2 +- 5 files changed, 103 insertions(+), 85 deletions(-) create mode 100644 app/index/route/app.php create mode 100644 public/doc/路由.md diff --git a/app/admin/controller/Index.php b/app/admin/controller/Index.php index 213b7fc..02d6430 100644 --- a/app/admin/controller/Index.php +++ b/app/admin/controller/Index.php @@ -79,88 +79,4 @@ class Index extends AuthController } return app("json")->success($menuList); } - - /** - * 生成sitemap.xml - */ - public function sitemap() - { - - //获取域名 - $domain = Request::domain(); - //获取页码 - $page = input('page/d')?:1; - $str = ''; - $pagesize = 100; - if ($page == 1) { - if (file_exists('sitemap.xml')) - unlink('sitemap.xml'); - $str .= ''; - $str .= ''; - //首页 - $str .= ''; - $str .= '' . $domain . ''; - $str .= '' . date("Y-m-d\TH:i:s+00:00",time()). ''; - $str .= 'daily'; - $str .= '1.0'; - $str .= ''; - - //获取文章分类url - $documentCategoryModel = new DocumentCategoryModel(); - $categoryInfo = $documentCategoryModel->field('id,alias,title,create_time') - ->where('status', 1) - ->page($page, $pagesize) - ->order('id desc')->select(); - foreach ($categoryInfo as $v) { - $str .= ''; - $str .= '' . url('index/article/lists',["id"=>$v['id']],".html",$domain) . ''; - $str .= '' . date("Y-m-d\TH:i:s+00:00",strtotime($v['create_time'])) . ''; - $str .= 'always'; - $str .= '0.8'; - $str .= ''; - } - } - //获取文章URL - $documentModel = new DocumentModel(); - $documentInfo = $documentModel->field('id,alias,create_time') - ->where('display', 1) - ->page($page, $pagesize) - ->order('id desc')->select(); - - foreach ($documentInfo as $v) { - $str .= ''; - $str .= '' . url('/article/detail',["id"=>$v["alias"]?:$v['id']],".html",$domain) . ''; - $str .= ''.date("Y-m-d\TH:i:s+00:00",strtotime($v['create_time'])) .''; - $str .= 'monthly'; - $str .= '0.6'; - $str .= ''; - } - - //获取文章标签页url - $documentModel = new TagModel(); - $documentInfo = $documentModel->field('name,create_time') - ->page($page, $pagesize) - ->group('name') - ->order('name desc')->select(); - - foreach ($documentInfo as $v) { - $str .= ''; - $str .= '' . url('/article/tag',["t"=>$v["name"]],".html",$domain) . ''; - $str .= ''.date("Y-m-d\TH:i:s+00:00",strtotime($v['create_time'])) .''; - $str .= 'monthly'; - $str .= '0.6'; - $str .= ''; - } - - if (count($documentInfo) < $pagesize) { - $str .= ''; - return (!(file_put_contents('sitemap.xml', $str, FILE_APPEND | LOCK_EX))) ? - $this->failedNotice("站点地图更新失败!", "/admin/") : - $this->successfulNotice("站点地图全部更新完成!", "/admin/"); - } - //写入 - return (!(file_put_contents('sitemap.xml', $str, FILE_APPEND | LOCK_EX))) ? - $this->failedNotice("站点地图更新失败!", "/admin/") : - $this->successfulNotice('站点地图正在生成,请稍后(' . $page . ')...', 'sitemap?page=' . ($page + 1)); - } } \ No newline at end of file diff --git a/app/index/controller/Index.php b/app/index/controller/Index.php index ed56613..f190549 100644 --- a/app/index/controller/Index.php +++ b/app/index/controller/Index.php @@ -5,6 +5,8 @@ namespace app\index\controller; use app\admin\extend\Util as Util; use app\common\constant\Data; use app\common\model\Document; +use app\common\model\Document as DocumentModel; +use app\common\model\DocumentCategory as DocumentCategoryModel; use app\common\model\FriendLink as friendLinkModel; use app\common\model\MessageForm as MessageFormModel; use app\common\model\Tag as TagModel; @@ -13,6 +15,7 @@ use app\Request; use think\db\exception\DataNotFoundException; use think\db\exception\DbException; use think\db\exception\ModelNotFoundException; +use think\facade\Filesystem; use think\facade\Log; /** @@ -188,4 +191,78 @@ class Index extends Base ]); return app("json")->layui(TagModel::getList($where)); } + + /** + * 生成sitemap.xml + * @return \think\Response + * @throws DataNotFoundException + * @throws DbException + * @throws ModelNotFoundException + */ + public function sitemap(): \think\Response + { + //获取域名 + $domain = request()->domain(); + $cache_key = "sitemap.xml"; + //先获取缓存是否存在 + if ($content = cache($cache_key)){ + return response($content)->header(['Content-Type'=>'application/xml']); + } + // 通过输出缓冲区内容,生成XML + $str = ''; + $str.= ''; + //首页 + $str .= ''; + $str .= '' . $domain . ''; + $str .= '' . date("Y-m-d\TH:i:s+00:00",time()). ''; + $str .= 'daily'; + $str .= '1.0'; + $str .= ''; + //获取文章分类url + $documentCategoryModel = new DocumentCategoryModel(); + $categoryInfo = $documentCategoryModel->field('id,alias,title,create_time') + ->where('status', 1) + ->order('id desc')->select(); + foreach ($categoryInfo as $v) { + $str .= ''; + $str .= '' . url('index/article/lists',["id"=>$v['id']],".html",$domain) . ''; + $str .= '' . date("Y-m-d\TH:i:s+00:00",strtotime($v['create_time'])) . ''; + $str .= 'always'; + $str .= '0.8'; + $str .= ''; + } + //获取文章URL + $documentModel = new DocumentModel(); + $documentInfo = $documentModel->field('id,alias,create_time') + ->where('display', 1) + ->order('id desc')->select(); + + foreach ($documentInfo as $v) { + $str .= ''; + $str .= '' . url('/article/detail',["id"=>$v["alias"]?:$v['id']],".html",$domain) . ''; + $str .= ''.date("Y-m-d\TH:i:s+00:00",strtotime($v['create_time'])) .''; + $str .= 'monthly'; + $str .= '0.6'; + $str .= ''; + } + + //获取文章标签页url + $documentModel = new TagModel(); + $documentInfo = $documentModel->field('name,create_time') + ->group('name') + ->order('name desc')->select(); + + foreach ($documentInfo as $v) { + $str .= ''; + $str .= '' . url('/article/tag',["t"=>$v["name"]],".html",$domain) . ''; + $str .= ''.date("Y-m-d\TH:i:s+00:00",strtotime($v['create_time'])) .''; + $str .= 'monthly'; + $str .= '0.6'; + $str .= ''; + } + $str .= ''; + cache($cache_key,$str,24*60*60);//每日更新 + // 将内容输出到浏览器 + return response($str)->header(['Content-Type'=>'application/xml']); + } } \ No newline at end of file diff --git a/app/index/route/app.php b/app/index/route/app.php new file mode 100644 index 0000000..04a589d --- /dev/null +++ b/app/index/route/app.php @@ -0,0 +1,18 @@ + +// +---------------------------------------------------------------------- +use think\facade\Route; + +Route::get('think', function () { + return 'hello,ThinkPHP6!'; +}); + +Route::get('hello/:name', 'index/hello'); +Route::rule('/sitemap.xml', 'index/sitemap'); \ No newline at end of file diff --git a/public/doc/路由.md b/public/doc/路由.md new file mode 100644 index 0000000..ed96c53 --- /dev/null +++ b/public/doc/路由.md @@ -0,0 +1,7 @@ +##短网址访问 + 在 app/index/route/app.php最底下添加 +```php +Route::get(':id', 'Article/detail'); +``` +即可实现 demain/xxx 对文章的访问 + diff --git a/route/app.php b/route/app.php index d8e09e3..c7b4229 100644 --- a/route/app.php +++ b/route/app.php @@ -14,4 +14,4 @@ Route::get('think', function () { return 'hello,ThinkPHP6!'; }); -Route::get('hello/:name', 'index/hello'); +Route::get('hello/:name', 'index/hello'); \ No newline at end of file