فهرست منبع

add func sitemap generator

Vien 5 سال پیش
والد
کامیت
3ac38a093e
3فایلهای تغییر یافته به همراه90 افزوده شده و 1 حذف شده
  1. 9 0
      README.md
  2. 79 0
      app/Console/Commands/Sitemap.php
  3. 2 1
      composer.json

+ 9 - 0
README.md

@@ -21,6 +21,7 @@ Github: [laravel-blog](https://github.com/luvvien/laravel-blog) ,欢迎Star。
 
 ## 更新
 
+- 20200520 添加sitemap自动生成功能 更新后需要执行 `composer update` 安装依赖,项目目录下执行 `php artisan sitemap:generate` 即可生成
 - 20200520 功能添加(图片自动水印),添加粉色主题 更新后需要执行 `php artisan migrate` 和 `php artisan db:seed --class=SettingSeeder`
 - 20200429 后台功能添加(图片上传功能) 更新后需要执行 `php artisan migrate`
 - 20200418 后台功能添加(网站设置,添加开关和统计代码等后台管理)更新后需要执行 `php artisan migrate` 和 `php artisan db:seed --class=SiteSwitchSeeder`
@@ -189,6 +190,14 @@ server {
 php artisan push:baidu
 ```
 
+#### SiteMap自动生成工具
+
+使用前请先在`.env`中配置好`APP_URL`,最终会在`public`下生成名为`sitemap_20200520`的sitemap文件
+
+```
+php artisan sitemap:generate
+```
+
 ## 讨论群
 
 加微信拉群: luvvien (欢迎开发者,技术爱好者,站长加入)

+ 79 - 0
app/Console/Commands/Sitemap.php

@@ -0,0 +1,79 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\App;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\URL;
+
+class Sitemap extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'sitemap:generate';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'generate sitemap.';
+
+    /**
+     * Create a new command instance.
+     *
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    protected function generate()
+    {
+        $domain = config('APP_URL', '');
+        // create new sitemap object
+        $sitemap = App::make("sitemap");
+
+        // add items to the sitemap (url, date, priority, freq)
+        $sitemap->add(URL::to('/'), date("Y-m-d\TH:i:s+00:00", time()), '0.8', 'daily');
+
+        $articles = DB::select("select id, slug from blog_articles");
+        $tags = DB::select("select id, tag_name from blog_tags");
+        $categories = DB::select("select id, cate_name from blog_categories");
+
+        foreach ($articles as $article) {
+            $url = $domain.'/'.$article->slug;
+            $sitemap->add(URL::to($url), date("Y-m-d\TH:i:s+00:00", time()), '0.8', 'daily');
+        }
+
+        foreach ($tags as $tag) {
+            $url = $domain.'/tag/'.$tag->tag_name;
+            $sitemap->add(URL::to($url), date("Y-m-d\TH:i:s+00:00", time()), '0.8', 'daily');
+        }
+
+        foreach ($categories as $category) {
+            $url = $domain.'/category/'.$category->cate_name;
+            $sitemap->add(URL::to($url), date("Y-m-d\TH:i:s+00:00", time()), '0.8', 'daily');
+        }
+
+
+        // generate your sitemap (format, filename)
+        $sitemap->store('xml', 'sitemap_20200520');
+
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $this->info('Generating sitemap...');
+        $this->generate();
+    }
+}

+ 2 - 1
composer.json

@@ -14,7 +14,8 @@
         "intervention/image": "^2.4",
         "laravel/framework": "5.8.*",
         "laravel/tinker": "^1.0",
-        "yasmuru/ys-tinify-laravel": "^1.0"
+        "yasmuru/ys-tinify-laravel": "^1.0",
+        "laravelium/sitemap": "3.1.*"
     },
     "require-dev": {
         "beyondcode/laravel-dump-server": "^1.0",