Просмотр файла app/Http/Controllers/SitemapController.php

Размер файла: 5.35Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Http\Controllers;
  6.  
  7. use App\Models\Article;
  8. use App\Models\Down;
  9. use App\Models\News;
  10. use App\Models\Topic;
  11. use Illuminate\Database\Query\JoinClause;
  12. use Illuminate\View\View;
  13.  
  14. class SitemapController extends Controller
  15. {
  16. /**
  17. * @var array
  18. */
  19. public $pages = [
  20. 'news',
  21. 'articles',
  22. 'topics',
  23. 'downs',
  24. ];
  25.  
  26. /**
  27. * Генерируем главную страницу
  28. *
  29. * @return View
  30. */
  31. public function index(): View
  32. {
  33. $locs = [];
  34.  
  35. foreach ($this->pages as $page) {
  36. $locs[] = [
  37. 'loc' => config('app.url') . '/sitemap/' . $page . '.xml',
  38. 'lastmod' => date('c', SITETIME),
  39. ];
  40. }
  41.  
  42. return view('sitemap/index', compact('locs'));
  43. }
  44.  
  45. /**
  46. * Вызывает страницу
  47. *
  48. * @param string $page
  49. *
  50. * @return View
  51. */
  52. public function page(string $page): View
  53. {
  54. if (! in_array($page, $this->pages, true)) {
  55. abort(404);
  56. }
  57.  
  58. return $this->$page();
  59. }
  60.  
  61. /**
  62. * Генерируем статьи
  63. *
  64. * @return View
  65. */
  66. private function articles(): View
  67. {
  68. $articles = Article::query()
  69. ->selectRaw('articles.*, max(c.created_at) as last_time')
  70. ->leftJoin('comments as c', static function (JoinClause $join) {
  71. $join->on('articles.id', 'c.relate_id')
  72. ->where('relate_type', Article::$morphName);
  73. })
  74. ->groupBy('articles.id')
  75. ->orderByDesc('last_time')
  76. ->get();
  77.  
  78. $locs = [];
  79.  
  80. foreach ($articles as $article) {
  81. $changeTime = ($article->last_time > $article->created_at) ? $article->last_time : $article->created_at;
  82.  
  83. // Обновлено менее 1 месяца
  84. $new = SITETIME < strtotime('+1 month', $changeTime);
  85.  
  86. $locs[] = [
  87. 'loc' => config('app.url') . '/articles/' . $article->id,
  88. 'lastmod' => date('c', $changeTime),
  89. 'changefreq' => $new ? 'weekly' : 'monthly',
  90. 'priority' => $new ? '1.0' : '0.5',
  91. ];
  92. }
  93.  
  94. return view('sitemap/url', compact('locs'));
  95. }
  96.  
  97. /**
  98. * Генерируем новости
  99. *
  100. * @return View
  101. */
  102. private function news(): View
  103. {
  104. $newses = News::query()
  105. ->selectRaw('news.*, max(c.created_at) as last_time')
  106. ->leftJoin('comments as c', static function (JoinClause $join) {
  107. $join->on('news.id', 'c.relate_id')
  108. ->where('relate_type', News::$morphName);
  109. })
  110. ->groupBy('news.id')
  111. ->orderByDesc('last_time')
  112. ->get();
  113.  
  114. $locs = [];
  115.  
  116. foreach ($newses as $news) {
  117. $changeTime = ($news->last_time > $news->created_at) ? $news->last_time : $news->created_at;
  118.  
  119. // Обновлено менее 1 месяца
  120. $new = SITETIME < strtotime('+1 month', $changeTime);
  121.  
  122. $locs[] = [
  123. 'loc' => config('app.url') . '/news/' . $news->id,
  124. 'lastmod' => date('c', $changeTime),
  125. 'changefreq' => $new ? 'weekly' : 'monthly',
  126. 'priority' => $new ? '1.0' : '0.5',
  127. ];
  128. }
  129.  
  130. return view('sitemap/url', compact('locs'));
  131. }
  132.  
  133. /**
  134. * Генерируем темы форума
  135. *
  136. * @return View
  137. */
  138. private function topics(): View
  139. {
  140. $topics = Topic::query()->orderByDesc('updated_at')->limit(15000)->get();
  141.  
  142. $locs = [];
  143.  
  144. foreach ($topics as $topic) {
  145. // Обновлено менее 1 месяца
  146. $new = SITETIME < strtotime('+1 month', $topic->updated_at);
  147.  
  148. $locs[] = [
  149. 'loc' => config('app.url') . '/topics/' . $topic->id,
  150. 'lastmod' => date('c', $topic->updated_at),
  151. 'changefreq' => $new ? 'weekly' : 'monthly',
  152. 'priority' => $new ? '1.0' : '0.5',
  153. ];
  154. }
  155. return view('sitemap/url', compact('locs'));
  156. }
  157.  
  158. /**
  159. * Генерируем загрузки
  160. *
  161. * @return View
  162. */
  163. private function downs(): View
  164. {
  165. $downs = Down::query()
  166. ->selectRaw('downs.*, max(c.created_at) as last_time')
  167. ->leftJoin('comments as c', static function (JoinClause $join) {
  168. $join->on('downs.id', 'c.relate_id')
  169. ->where('relate_type', Down::$morphName);
  170. })
  171. ->groupBy('downs.id')
  172. ->orderByDesc('last_time')
  173. ->get();
  174.  
  175. $locs = [];
  176. foreach ($downs as $down) {
  177. $changeTime = ($down->last_time > $down->created_at) ? $down->last_time : $down->created_at;
  178.  
  179. // Обновлено менее 1 месяца
  180. $new = SITETIME < strtotime('+1 month', $changeTime);
  181.  
  182. $locs[] = [
  183. 'loc' => config('app.url') . '/downs/' . $down->id,
  184. 'lastmod' => date('c', $changeTime),
  185. 'changefreq' => $new ? 'weekly' : 'monthly',
  186. 'priority' => $new ? '1.0' : '0.5',
  187. ];
  188. }
  189.  
  190. return view('sitemap/url', compact('locs'));
  191. }
  192. }