isStaging($request)) {
$body = "User-agent: *\nDisallow: /\n";
} else {
$base = $request->getSchemeAndHttpHost();
$body = "User-agent: *\n"
. "Disallow: /admin\n"
. "Disallow: /api\n"
. "Allow: /\n\n"
. "Sitemap: {$base}/sitemap.xml\n";
}
return new Response($body, Response::HTTP_OK, ['Content-Type' => 'text/plain; charset=UTF-8']);
}
#[Route('/sitemap.xml', name: 'seo_sitemap', methods: ['GET'])]
public function sitemap(Request $request): Response
{
$base = $request->getSchemeAndHttpHost();
$xml = '' . "\n"
. '' . "\n"
. $this->urlNode("{$base}/", 'weekly', '1.0')
. $this->urlNode($base . '/' . rawurlencode(AboutController::SLUG), 'yearly', '0.5');
// اسلاگها فارسیاند و باید percent-encode شوند، وگرنه XML نامعتبر است.
foreach ($this->landings->all() as $page) {
$xml .= $this->urlNode($base . '/' . rawurlencode($page->slug), 'monthly', '0.8');
}
$xml .= '' . "\n";
return new Response($xml, Response::HTTP_OK, ['Content-Type' => 'application/xml; charset=UTF-8']);
}
private function urlNode(string $loc, string $changefreq, string $priority): string
{
return ' ' . "\n"
. ' ' . htmlspecialchars($loc, ENT_XML1) . '' . "\n"
. " {$changefreq}\n"
. " {$priority}\n"
. ' ' . "\n";
}
private function isStaging(Request $request): bool
{
$host = $request->getHost();
return str_ends_with($host, '.ddev.site')
|| str_contains($host, 'localhost')
|| str_starts_with($host, '127.0.0.1');
}
}