Melodia
2 months ago
189
3
General

Spatie sitemap generates xml file but no link is added

Posted 2 months ago by Melodia

How do I use sitemap in laravel?

I'm trying to use spatie sitemap package but am running into a few problems. First, when I added the following code and hit the url, the sitemap file is created but not url is created:

Route::get('map', function() {

    SitemapGenerator::create('http://127.0.0.1:8000')->writeToFile('sitemap.xml');
    return "sitemap generated";
    
});

So, I decided to add them via the manual option:

Route::get('sitemap', function () {
    SitemapGenerator::create('http://127.0.0.1:8000')->getSitemap()
    ->add(Url::create('/')->setPriority(0.5))
    ->add(Url::create('/admin/{locale}/post/{post}')->setPriority(0.5))
    ->add(Url::create('/languages')->setPriority(0.5))
    ->writeToFile('sitemap.xml');
    return "Sitemap Generated";
});

and the sitemap looks like this:

<?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>http://127.0.0.1:8000</loc>
        <lastmod>2020-01-13T16:01:24+00:00</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.5</priority>
    </url>
    <url>
        <loc>http://127.0.0.1:8000/{language}/post/{post}</loc>
        <lastmod>2020-01-13T16:01:24+00:00</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.5</priority>
    </url>
    <url>
        <loc>http://127.0.0.1:8000/languages</loc>
        <lastmod>2020-01-13T16:01:24+00:00</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.5</priority>
    </url>
</urlset>

Question 1: any reason why the sitemap did not generate with the first code?

Question 2: How does it really work for URL with parameters? I might have multiple posts from in the database, so ideally there should be a url generated for each post.

Please sign in or create an account to participate in this conversation.