【问题标题】:How to generate a sitemap.xml using laravel-sitemap without images url?如何使用没有图像 url 的 laravel-sitemap 生成 sitemap.xml?
【发布时间】:2020-10-12 22:42:36
【问题描述】:

我正在使用spatie 生成sitemap.xml。它按预期正常工作。但根据一些要求,我不想在 sitemap.xml 中包含任何图像 url。

这是我的代码 -

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 中删除这些图片链接?

感谢您的帮助。

【问题讨论】:

    标签: php laravel xml-sitemap


    【解决方案1】:

    请遵循文档的这一部分 - spatie。这不是专门省略图片,但如果您的图片链接的网址中有任何特定格式 - 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');
        }
    

    希望这对你有用。

    【讨论】:

      猜你喜欢
      • 2022-07-04
      • 1970-01-01
      • 1970-01-01
      • 2011-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多