SitemapController.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Support\Facades\Cache;
  4. use App\Models\DistProduct;
  5. use App\Models\SitePage as DistPages;
  6. use App\Models\DistVideo;
  7. class SitemapController extends Controller
  8. {
  9. public function index()
  10. {
  11. // 定义缓存键和时长
  12. $cacheKey = "sitemap::sitemap_index_" . getDistId();
  13. $cacheDuration = 60 * 60; // 缓存时间(秒):60 分钟
  14. // 获取缓存数据或重新生成
  15. $sitemapData = Cache::remember($cacheKey, $cacheDuration, function () {
  16. // 查询数据
  17. $dist_products = DistProduct::where('enabled', 1)->where('dist_id', getDistId())->get();
  18. $dist_pages = DistPages::where('status', 1)->where('dist_id', getDistId())->get();
  19. $dist_videos = DistVideo::where('enabled', 1)->where('dist_id', getDistId())->get();
  20. // 返回查询结果
  21. return [
  22. 'dist_products' => $dist_products,
  23. 'dist_pages' => $dist_pages,
  24. 'dist_videos' => $dist_videos,
  25. ];
  26. });
  27. // 创建视图并返回响应
  28. return response()->view('sitemap.index', $sitemapData)->header('Content-Type', 'application/xml');
  29. }
  30. }