|
@@ -0,0 +1,94 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+
|
|
|
|
|
+namespace App\Http\Controllers;
|
|
|
|
|
+
|
|
|
|
|
+use Illuminate\Support\Facades\Cache;
|
|
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * sitemap.xml 생성
|
|
|
|
|
+ *
|
|
|
|
|
+ * 기존에는 public/sitemap.xml 정적 파일을 손으로 갱신했는데, 목록 페이지 14건만 담겨 있고
|
|
|
|
|
+ * 개별 게시글이 한 건도 없었다(lastmod 도 2026-01-10 에 멈춰 있었다).
|
|
|
|
|
+ * 블로그에서 색인 가치가 가장 높은 것이 게시글 본문이므로 DB 에서 직접 생성한다.
|
|
|
|
|
+ *
|
|
|
|
|
+ * 공개 판정 조건은 Board 모델의 기존 공개 목록 쿼리와 동일하게 맞춘다.
|
|
|
|
|
+ * (BRD.is_display = 1, PST.is_delete = 0, PST.is_secret = 0, PST.is_personal = 0)
|
|
|
|
|
+ */
|
|
|
|
|
+class SitemapController extends Controller
|
|
|
|
|
+{
|
|
|
|
|
+ // 게시글이 늘어나도 매 요청마다 전체 조회를 하지 않도록 캐시한다.
|
|
|
|
|
+ private const CACHE_KEY = 'sitemap.xml';
|
|
|
|
|
+ private const CACHE_SECONDS = 3600;
|
|
|
|
|
+
|
|
|
|
|
+ public function index()
|
|
|
|
|
+ {
|
|
|
|
|
+ $xml = Cache::remember(self::CACHE_KEY, self::CACHE_SECONDS, function () {
|
|
|
|
|
+ return $this->build();
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ return response($xml, 200)->header('Content-Type', 'application/xml; charset=utf-8');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function build(): string
|
|
|
|
|
+ {
|
|
|
|
|
+ // 레이아웃의 canonical(url()->current())과 같은 방식으로 요청 기준 주소를 쓴다.
|
|
|
|
|
+ // config('app.url') 은 환경파일 값에 좌우되어(로컬은 local-blog.web.or.kr) 어긋날 수 있다.
|
|
|
|
|
+ $baseUrl = rtrim(url('/'), '/');
|
|
|
|
|
+ $today = date('Y-m-d');
|
|
|
|
|
+
|
|
|
|
|
+ $xml = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
|
|
|
|
|
+ $xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . PHP_EOL;
|
|
|
|
|
+
|
|
|
|
|
+ // 고정 페이지
|
|
|
|
|
+ $xml .= $this->url("{$baseUrl}/", $today, 'daily', '1.0');
|
|
|
|
|
+ $xml .= $this->url("{$baseUrl}/recently", $today, 'daily', '0.8');
|
|
|
|
|
+ $xml .= $this->url("{$baseUrl}/tag", $today, 'daily', '0.6');
|
|
|
|
|
+
|
|
|
|
|
+ // 문서 페이지. tb_document 에 노출 여부 컬럼을 확인하지 못해 현재 공개된 두 건만 둔다.
|
|
|
|
|
+ $xml .= $this->url("{$baseUrl}/document/profile", $today, 'monthly', '0.5');
|
|
|
|
|
+ $xml .= $this->url("{$baseUrl}/document/proposal", $today, 'monthly', '0.5');
|
|
|
|
|
+
|
|
|
|
|
+ // 게시판 목록
|
|
|
|
|
+ $boards = DB::table('tb_board')
|
|
|
|
|
+ ->where('is_display', '=', 1)
|
|
|
|
|
+ ->orderBy('sort')
|
|
|
|
|
+ ->pluck('code');
|
|
|
|
|
+
|
|
|
|
|
+ foreach ($boards as $code) {
|
|
|
|
|
+ $xml .= $this->url("{$baseUrl}/board/{$code}", $today, 'daily', '0.8');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 게시글. 공지/전체공지도 실제로 열리는 페이지이므로 함께 넣는다.
|
|
|
|
|
+ DB::table('tb_post')
|
|
|
|
|
+ ->join('tb_board', 'tb_board.id', '=', 'tb_post.board_id')
|
|
|
|
|
+ ->where('tb_board.is_display', '=', 1)
|
|
|
|
|
+ ->where('tb_post.is_delete', '=', 0)
|
|
|
|
|
+ ->where('tb_post.is_secret', '=', 0)
|
|
|
|
|
+ ->where('tb_post.is_personal', '=', 0)
|
|
|
|
|
+ ->select('tb_board.code', 'tb_post.id', 'tb_post.updated_at', 'tb_post.created_at')
|
|
|
|
|
+ ->orderBy('tb_post.id')
|
|
|
|
|
+ ->chunk(1000, function ($posts) use (&$xml, $baseUrl, $today) {
|
|
|
|
|
+ foreach ($posts as $post) {
|
|
|
|
|
+ $modified = $post->updated_at ?: $post->created_at;
|
|
|
|
|
+ $lastModified = $modified ? date('Y-m-d', strtotime($modified)) : $today;
|
|
|
|
|
+
|
|
|
|
|
+ $xml .= $this->url("{$baseUrl}/board/{$post->code}/{$post->id}", $lastModified, 'weekly', '0.6');
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ $xml .= '</urlset>' . PHP_EOL;
|
|
|
|
|
+
|
|
|
|
|
+ return $xml;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function url(string $location, string $lastModified, string $changeFrequency, string $priority): string
|
|
|
|
|
+ {
|
|
|
|
|
+ return ' <url>' . PHP_EOL
|
|
|
|
|
+ . ' <loc>' . htmlspecialchars($location, ENT_XML1) . '</loc>' . PHP_EOL
|
|
|
|
|
+ . ' <lastmod>' . $lastModified . '</lastmod>' . PHP_EOL
|
|
|
|
|
+ . ' <changefreq>' . $changeFrequency . '</changefreq>' . PHP_EOL
|
|
|
|
|
+ . ' <priority>' . $priority . '</priority>' . PHP_EOL
|
|
|
|
|
+ . ' </url>' . PHP_EOL;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|