fn_b/app/Http/Controllers/SitemapController.php

38 lines
1.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Articles;
use App\Models\Categories;
use Illuminate\Http\Response;
class SitemapController extends Controller
{
public function index(): Response
{
// Use the same expiry rule as article metadata, including on SQLite in isolated tests.
$cArticles = Articles::where('iStatus', 1)
->where('created_at', '<=', now())
->whereHas('oCategory', fn ($q) => $q->where('iStatus', 1))
->with('oCategory')
->orderBy('created_at', 'desc')
->get()->reject(fn ($article) => $article->bIsSeoExpired());
$visible = fn ($q) => $q->where('iStatus', 1)->where('created_at', '<=', now());
$cCategories = Categories::whereIn('sSlug', ['client', 'advanced'])
->where('iParentId', 0)->where('iStatus', 1)
->where(function ($q) use ($visible) {
$q->whereHas('aArticles', $visible)->orWhereHas('aChildren', function ($children) use ($visible) {
$children->where('iStatus', 1)->whereHas('aArticles', $visible);
});
})->orderBy('sSlug')->get();
$sXmlContent = view('sitemap', [
'cArticles' => $cArticles,
'cCategories' => $cCategories,
])->render();
return response($sXmlContent, 200)->header('Content-Type', 'text/xml');
}
}