<?php

namespace App\Http\Controllers;


use App\Models\Article;
use App\Models\Category;
use App\Models\Staff;
use Illuminate\Support\Facades\App;

class SitemapController extends Controller
{

    public function index()
    {
        return view('category.sitemap');
    }

    /**
     * Notes: 保存
     *
     * @Author: 玄尘
     * @Date: 2022/6/30 14:29
     * @return mixed
     */
    public function store()
    {
        $url = [
            url('/'),
            url('staff'),
        ];

        $sitemap = App::make("sitemap");// 创建一个生成站点地图的对象

        $sitemap->setCache('laravel.sitemap', 3600);// 设置缓存

        $categories = Category::where('status', 1)->get();
        foreach ($categories as $category) {
            $url[] = $category->link;
        }

        $articles = Article::shown()->get();
        foreach ($articles as $article) {
            $url[] = $article->link;
        }


        $staffs = Staff::shown()->get();
        foreach ($staffs as $staff) {
            $url[] = $staff->link;
        }


        $dateTime = date('Y-m-d H:i:s');
        foreach ($url as $k => $v) {
            $sitemap->add($v, $dateTime, '1.0', 'daily');
        }

        // 渲染站点地图(options: 'xml' (default), 'html', 'txt', 'ror-rss', 'ror-rdf')
        return $sitemap->store('xml');
    }
}
