简体   繁体   English

如何使用没有图像 url 的 laravel-sitemap 生成 sitemap.xml?

[英]How to generate a sitemap.xml using laravel-sitemap without images url?

I am using spatie to generate sitemap.xml.我正在使用spatie生成 sitemap.xml。 It is working properly as it is expected.它按预期正常工作。 But as per some requirement I would like to not include any image url in sitemap.xml.但根据某些要求,我不想在 sitemap.xml 中包含任何图像 url。

This is my code -这是我的代码 -

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');
    }

Currently my sitemap.xml something looks like this with image links -目前我的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>

This there any way to remove those image links from sitemap.xml?这有什么方法可以从 sitemap.xml 中删除这些图像链接?

Appreciate for help.感谢您的帮助。

Please follow this part of document - spatie .请按照文档的这一部分进行操作 This is not specifically omit images but if there is any specific format in your url for images link - http://www.example.com/uploads then try to get segment(1) and compare so this part of documentation will work.这不是专门省略图像,但如果您的 url 中有任何特定格式的图像链接 - http://www.example.com/uploads然后尝试获取segment(1)并进行比较,以便这部分文档将起作用。

In your case as an example -以您为例-

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');
    }

Hope this will work for you.希望这对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM