我正在使用spatie生成sitemap.xml。它像预期的那样正常工作。但根据一些要求,我不想在sitemap.xml中包括任何图像网址。
这是我的代码-
public function sitemap(){
$link = Website::where('adminId', Auth::user()->id)->value('link');
$path = public_path('uploads/'.Auth::user()->id.'/sitemap.xml');
if (file_exists($path)) {
File::delete($path);
fopen($path, 'w');
} else {
fopen($path, 'w');
}
SitemapGenerator::create($link)->writeToFile($path);
return response()->json('success');
}
目前我的sitemap.xml
与图片链接类似--
<url>
<loc>http://www.example.com/uploads/3/page/anahera/1591769129-4412.jpeg</loc>
<lastmod>2020-06-23T11:22:59+12:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>http://www.example.com/uploads/3/page/anahera/1591769136-7646.jpeg</loc>
<lastmod>2020-06-23T11:22:59+12:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
有什么方法可以从sitemap.xml中删除这些图片链接吗?
感谢您的帮助。
发布于 2020-06-23 07:50:35
请遵循document - spatie的这一部分。这并不是特别省略图像,但是如果您的url中有图像链接http://www.example.com/uploads
的特定格式,那么请尝试获取segment(1)
并进行比较,这样文档的这一部分就可以工作。
以你的例子为例-
public function sitemap(){
$link = Website::where('adminId', Auth::user()->id)->value('link');
$path = public_path('uploads/'.Auth::user()->id.'/sitemap.xml');
if (file_exists($path)) {
File::delete($path);
fopen($path, 'w');
} else {
fopen($path, 'w');
}
SitemapGenerator::create($link)->hasCrawled(function (Url $url) {
if ($url->segment(1) === 'uploads') {
return;
}
return $url;
})->writeToFile($path);
return response()->json('success');
}
希望这对你有用。
https://stackoverflow.com/questions/62525127
复制相似问题