This commit is contained in:
91
app/Http/Controllers/SitemapController.php
Normal file
91
app/Http/Controllers/SitemapController.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user