Files
tehnobox/app/Http/Controllers/SitemapController.php
ssww23 93a655235a
Some checks failed
Deploy / deploy (push) Has been cancelled
Initial commit
2026-03-10 00:55:37 +03:00

92 lines
3.1 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Category;
use App\Models\Product;
use Illuminate\Support\Collection;
class SitemapController extends Controller
{
public function index()
{
$latestProductUpdate = Product::query()->where('is_active', true)->max('updated_at');
$latestCategoryUpdate = Category::query()->where('is_active', true)->max('updated_at');
$catalogLastmod = collect([$latestProductUpdate, $latestCategoryUpdate])
->filter()
->map(fn ($value) => $value instanceof \Carbon\CarbonInterface ? $value : \Carbon\Carbon::parse((string) $value))
->max();
$catalogLastmodIso = $catalogLastmod?->toAtomString() ?? now()->toAtomString();
$urls = collect([
[
'loc' => route('home'),
'lastmod' => $catalogLastmodIso,
'changefreq' => 'daily',
'priority' => '1.0',
],
[
'loc' => route('catalog.index'),
'lastmod' => $catalogLastmodIso,
'changefreq' => 'daily',
'priority' => '0.9',
],
[
'loc' => route('pages.about'),
'lastmod' => now()->toAtomString(),
'changefreq' => 'monthly',
'priority' => '0.5',
],
[
'loc' => route('pages.contacts'),
'lastmod' => now()->toAtomString(),
'changefreq' => 'monthly',
'priority' => '0.5',
],
[
'loc' => route('pages.shipping-payment'),
'lastmod' => now()->toAtomString(),
'changefreq' => 'monthly',
'priority' => '0.5',
],
]);
$categoryUrls = Category::query()
->where('is_active', true)
->get()
->map(function (Category $category) {
return [
'loc' => route('catalog.category', $category),
'lastmod' => $category->updated_at?->toAtomString() ?? now()->toAtomString(),
'changefreq' => 'weekly',
'priority' => '0.8',
];
});
$productUrls = Product::query()
->where('is_active', true)
->get()
->map(function (Product $product) {
return [
'loc' => route('products.show', $product),
'lastmod' => $product->updated_at?->toAtomString() ?? now()->toAtomString(),
'changefreq' => 'weekly',
'priority' => '0.7',
];
});
$allUrls = $this->mergeUrls($urls, $categoryUrls, $productUrls);
return response()
->view('seo.sitemap', ['urls' => $allUrls])
->header('Content-Type', 'application/xml; charset=UTF-8');
}
private function mergeUrls(Collection ...$collections): Collection
{
return collect($collections)
->flatten(1)
->values();
}
}