Nativa PHP Core

Article

Ako sme urobili sitemap.xml bez frameworku

Ilustračný obrázok článku: Architecture, seo, sitemap, xml

Ako sme urobili sitemap.xml bez frameworku

Sitemap.xml je prvá vec, ktorú Googlebot hľadá. V Nativa Minimal generujeme dynamický XML sitemap priamo v PHP, žiadny framework, žiadne CLI generovanie, žiadne cache.

Prečo dynamický?

Statický sitemap vyžaduje build krok. Keď pridáte článok, musíte spustiť generate-sitemap. Pri dynamickom sitemap sa obsah aktualizuje automaticky, každý request generuje aktuálny XML.

Štruktúra sitemapu

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:xhtml="http://www.w3.org/1999/xhtml">
  <url>
    <loc>https://example.com/</loc>
    <changefreq>weekly</changefreq>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>https://example.com/blog/hello-world</loc>
    <lastmod>2026-08-04</lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.8</priority>
    <xhtml:link rel="alternate" hreflang="sk" href="..."/>
    <xhtml:link rel="alternate" hreflang="en" href="..."/>
  </url>
</urlset>

Implementácia

Sitemap sa generuje v jednej akcii,SitemapAction:

#[Route(path: '/sitemap.xml', method: 'GET', name: 'sitemap')]
final class SitemapAction
{
    public function handle(Request $request): Response
    {
        $base = $request->absoluteUrl('');

        $xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
            . '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"'
            . ' xmlns:xhtml="http://www.w3.org/1999/xhtml">' . "\n"
            . $this->staticEntries($base)
            . $this->articleEntries($base)
            . $this->pageEntries($base)
            . $this->authorEntries($base)
            . '</urlset>' . "\n";

        return Response::xml($xml, 'application/xml; charset=utf-8');
    }
}

Kľúčové: $request->absoluteUrl(''): generuje absolútnu URL z aktuálneho requestu. Funguje na localhost, na produkcii, cez proxy.

Čo zahŕňame

Statické stránky

  • Domovská stránka (obe locale)
  • Blog index (obe locale)
  • Feed endpoints (Atom + RSS)

Články

Každý článok v oboch locale s hreflang alternates:

$alternates = '<xhtml:link rel="alternate" hreflang="sk" href="...'
    . '"/>' . "\n";
$alternates .= '<xhtml:link rel="alternate" hreflang="en" href="...'
    . '"/>' . "\n";

Google tak vie, že example.com/blog/hello-world a example.com/en/blog/hello-world sú rovnaký článok v rôznych jazykoch.

Stránky

Statické stránky (iba published) s hreflang.

Autori

Author profily dosiahnuteľné z článkov, deduplikované.

Priority a changefreq

TypPriorityChangefreq
Domov1.0weekly
Blog index0.9daily
Články0.8weekly
Stránky0.6monthly
Autori0.6weekly
Feedy0.3daily

Priorita nie je záväzná, Google ju ignoruje. Ale changefreq a lastmod pomáhajú s crawl budgetom.

Bezpečnosť

Všetky hodnoty sú XML-escaped:

FrontendLayout::e($loc)  // escape &, <, >, ", '

Žiadny user input nedosiahne XML bez sanitizácie.

Testy

Sitemap má dedicated testy:

public function testSitemapListsArticles(): void
{
    $xml = (new SitemapAction())->handle(
        Request::create('GET', '/sitemap.xml')
    )->getContent();

    self::assertStringContainsString('<loc>', $xml);
    self::assertStringContainsString('hreflang="sk"', $xml);
    self::assertStringContainsString('hreflang="en"', $xml);
}

Zdroje

Comments (0)

No comments yet — be the first to react.

Reader questions (0)

No questions yet — be the first to ask.

Stuck on something in this article? Ask below. Your name is shown publicly; your email is never stored. A question appears here once a moderator approves it.