spatie / laravel-sitemap : 如何设置主页优先级

spatie / laravel-sitemap : How to set homepage priority

如何设置主页的优先级?

我尝试了很多东西,包括:

SitemapGenerator::create('https://www.example.com')
        ->hasCrawled(function (Url $url) {
            if ($url->segment(1) === 'https://www.example.com') {
                $url->setPriority(1)
                    ->setLastModificationDate(Carbon::create('2016', '1', '1'));
            }

            return $url;
        })
        ->writeToFile('sitemap.xml');
    }

设法找到答案:

//We set the homepage priority to 1
            ->hasCrawled(function (Url $url) {
                if ($url->segment(1) === env('https://www.example.com')) {
                    $url->setPriority(1);
                }
                return $url;
            })
            //We save the file and redirect to it
            ->writeToFile('sitemap.xml');
        return redirect(url('sitemap.xml'));

更好的是:

//We set the homepage priority to 1
            ->hasCrawled(function (Url $url) {
                if (is_null($url->segment(1))) {
                    $url->setPriority(1);
                }
                return $url;
            })
            //We save the file and redirect to it
            ->writeToFile('sitemap.xml');
        return redirect(url('sitemap.xml'));
    }