<?php
namespace App\Http\Controllers;
use App\Models\Service;
use App\Models\ProductCategory;
use App\Models\BlogPost;
use Illuminate\Http\Response;
class SitemapController extends Controller {
public function index(): Response {
$langs = ['en', 'ne', 'hi'];
$staticPages = ['/', '/about', '/services', '/products', '/brand-partners', '/faqs', '/blog', '/contact', '/why-sunauli'];
$urls = [];
foreach ($langs as $lang) {
foreach ($staticPages as $page) {
$urls[] = ['loc' => url("/{$lang}{$page}"), 'changefreq' => $page === '/' ? 'daily' : 'weekly', 'priority' => $page === '/' ? '1.0' : '0.8'];
}
foreach (Service::where('is_active', true)->get() as $service) {
$urls[] = ['loc' => url("/{$lang}/services/{$service->slug}"), 'changefreq' => 'monthly', 'priority' => '0.7'];
}
foreach (ProductCategory::where('is_active', true)->get() as $cat) {
$urls[] = ['loc' => url("/{$lang}/products/{$cat->slug}"), 'changefreq' => 'weekly', 'priority' => '0.6'];
}
}
foreach (BlogPost::published()->get() as $post) {
$urls[] = ['loc' => url("/en/blog/{$post->slug}"), 'changefreq' => 'monthly', 'priority' => '0.5', 'lastmod' => $post->updated_at->toDateString()];
}
$xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">' . "\n";
foreach ($urls as $url) {
$xml .= " <url>\n";
$xml .= " <loc>{$url['loc']}</loc>\n";
if (isset($url['lastmod'])) $xml .= " <lastmod>{$url['lastmod']}</lastmod>\n";
$xml .= " <changefreq>{$url['changefreq']}</changefreq>\n";
$xml .= " <priority>{$url['priority']}</priority>\n";
$xml .= " </url>\n";
}
$xml .= '</urlset>';
return response($xml, 200, ['Content-Type' => 'application/xml']);
}
public function robots(): Response {
$content = "User-agent: *\nAllow: /\nDisallow: /admin\nDisallow: /admin/*\n\nSitemap: " . url('/sitemap.xml');
return response($content, 200, ['Content-Type' => 'text/plain']);
}
}