fn_b/app/Http/Controllers/SitemapController.php

25 lines
1.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Articles;
use App\Models\Categories;
use App\Services\Seo;
use App\Services\ArticlesService;
use Illuminate\Http\Response;
class SitemapController extends Controller
{
public function index(): Response
{
$articles = Articles::publicVisible()->with('oCategory.oParent')->orderByDesc('created_at')->orderByDesc('id')->get();
$latest = app(ArticlesService::class)->getLatestFreeNodeSlug();
$home = $latest ? $articles->firstWhere('sSlug',$latest) : null;
$entries = $home ? [['url'=>Seo::url(),'modified'=>Seo::modified($home)]] : [];
foreach (Categories::where('iStatus',1)->with('oParent')->orderBy('id')->get() as $category) {
if ($category->iParentId && (!$category->oParent || !$category->oParent->iStatus)) continue;
$members = $articles->filter(fn($a)=>$a->iCategoryId === $category->id || ($category->iShowChildren && $a->oCategory->iParentId === $category->id));
if ($members->isNotEmpty()) $entries[] = ['url'=>Seo::url(Seo::categoryPath($category)),'modified'=>null];
}
foreach ($articles->filter(fn($a)=>Seo::indexable($a)) as $a) $entries[] = ['url'=>Seo::articleUrl($a),'modified'=>Seo::modified($a)];
return response()->view('sitemap',['entries'=>$entries])->header('Content-Type','application/xml; charset=UTF-8');
}
}