问题
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?
回答1:
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";
});
来源:https://stackoverflow.com/questions/54074213/spatie-laravel-sitemap-generated-xml-file-is-empty