Files
ai-web/app/Http/Controllers/Site/SeoController.php

57 lines
2.3 KiB
PHP
Raw Permalink Normal View History

2026-02-11 17:28:36 +08:00
<?php
declare(strict_types=1);
namespace App\Http\Controllers\Site;
use App\Http\Controllers\Controller;
use App\Models\AiModel;
use App\Models\Article;
use App\Models\Guide;
use App\Models\Tool;
use Illuminate\Http\Response;
class SeoController extends Controller
{
public function robots(): Response
{
$content = "User-agent: *\n";
$content .= "Allow: /\n";
$content .= "Disallow: /admin\n";
$content .= 'Sitemap: '.url('/sitemap.xml')."\n";
return response($content, 200, ['Content-Type' => 'text/plain; charset=UTF-8']);
}
public function sitemap(): Response
{
$urls = [
['loc' => url('/'), 'lastmod' => now()->toDateString(), 'priority' => '1.0'],
['loc' => route('tools.index'), 'lastmod' => now()->toDateString(), 'priority' => '0.9'],
['loc' => route('models.index'), 'lastmod' => now()->toDateString(), 'priority' => '0.9'],
['loc' => route('news.index'), 'lastmod' => now()->toDateString(), 'priority' => '0.8'],
['loc' => route('guides.index'), 'lastmod' => now()->toDateString(), 'priority' => '0.8'],
];
foreach (Tool::query()->published()->latest('updated_at')->limit(500)->get(['slug', 'updated_at']) as $item) {
$urls[] = ['loc' => route('tools.show', $item->slug), 'lastmod' => $item->updated_at?->toDateString(), 'priority' => '0.7'];
}
foreach (AiModel::query()->published()->latest('updated_at')->limit(500)->get(['slug', 'updated_at']) as $item) {
$urls[] = ['loc' => route('models.show', $item->slug), 'lastmod' => $item->updated_at?->toDateString(), 'priority' => '0.7'];
}
foreach (Article::query()->published()->latest('updated_at')->limit(500)->get(['slug', 'updated_at']) as $item) {
$urls[] = ['loc' => route('news.show', $item->slug), 'lastmod' => $item->updated_at?->toDateString(), 'priority' => '0.6'];
}
foreach (Guide::query()->published()->latest('updated_at')->limit(500)->get(['slug', 'updated_at']) as $item) {
$urls[] = ['loc' => route('guides.show', $item->slug), 'lastmod' => $item->updated_at?->toDateString(), 'priority' => '0.6'];
}
$xml = view('public.sitemap', ['urls' => $urls])->render();
return response($xml, 200, ['Content-Type' => 'application/xml; charset=UTF-8']);
}
}