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?
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";
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With