Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spatie Laravel sitemap generated xml file is empty

I am developing a Laravel application. Now, I am trying to implement the sitemap for my website using this package, https://github.com/spatie/laravel-sitemap. But when I generate sitemap.xml, no paths are included in the file.

I installed the package running the Composer command

composer require spatie/laravel-sitemap

Then I published the Composer.

php artisan vendor:publish --provider="Spatie\Sitemap\SitemapServiceProvider" --tag=config

In the routes/web.php, I added this.

Route::get('sitemap', function () {
    SitemapGenerator::create('http://app.localhost/')->writeToFile('sitemap.xml');
    return "Sitemap generated".
});

When I run the code and sitemap.xml is generated. When I opened the sitemap.xml, that is all I found in it.

<?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">
</urlset>

I have many routes in the web.php. What is wrong and how can fix it?

like image 638
Wai Yan Hein Avatar asked Jan 07 '19 12:01

Wai Yan Hein


2 Answers

You can manually add other urls like below and when you run it, the links will be added to your sitemap file:

use Spatie\Sitemap\SitemapGenerator;
use Spatie\Sitemap\Tags\Url;

 Route::get('sitemap', function () {
    SitemapGenerator::create('https://vesicash.com/')->getSitemap()
    ->add(Url::create('/link1')->setPriority(0.5))
    ->add(Url::create('/link2')->setPriority(0.5))
    ->add(Url::create('/link3')->setPriority(0.5))
    ->add(Url::create('/privacy')->setPriority(0.5))
    ->writeToFile('sitemap.xml');
    return "Sitemap Generated";
});
like image 51
Ehi Avatar answered Oct 06 '22 05:10

Ehi


This is how I solved this problem:

I just made sure that the APP_URL was set properly in .env file.

I added, for example: https://example.com/ and boom it worked.

This should also work on your local pc as well.

like image 31
Samson Banda Avatar answered Oct 06 '22 05:10

Samson Banda