安全联盟/laravel-sitemap

轻松创建和生成网站地图

3.3 2018-03-30 23:44 UTC

This package is auto-updated.

Last update: 2024-09-04 21:52:14 UTC


README

Latest Version on Packagist Software License Build Status Quality Score StyleCI Total Downloads

此包可以生成网站地图,无需手动添加URL。这是通过爬取您的整个网站来实现的。

use safaeean\Sitemap\SitemapGenerator;

SitemapGenerator::create('https://example.com')->writeToFile($path);

您也可以手动创建网站地图

use Carbon\Carbon;
use safaeean\Sitemap\Sitemap;
use safaeean\Sitemap\Tags\Url;

Sitemap::create()

    ->add(Url::create('/home')
        ->setLastModificationDate(Carbon::yesterday())
        ->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
        ->setPriority(0.1))

   ->add(...)

   ->writeToFile($path);

或者,您可以通过生成网站地图并添加更多链接来兼得两者之优

SitemapGenerator::create('https://example.com')
   ->getSitemap()
   ->add(Url::create('/extra-page')
        ->setLastModificationDate(Carbon::yesterday())
        ->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
        ->setPriority(0.1))

    ->add(...)

    ->writeToFile($path);

贺卡软件

您可以使用此包(它是MIT许可),但如果它进入了您的生产环境,我们非常欢迎您从家乡寄给我们明信片,提及您正在使用我们的哪个包。

我们的地址是:安全联盟,Samberstraat 69D,2060 安特卫普,比利时。

所有明信片都发布在我们的网站上

安装

首先,通过composer安装此包

composer require safaeean/laravel-sitemap

接下来:安装服务提供者

// config/app.php
'providers' => [
    ...
    safaeean\Sitemap\SitemapServiceProvider::class,
];

如果您想自动且频繁地更新您的网站地图,您需要执行一些额外步骤

配置

您可以通过覆盖爬虫的默认选项来更改默认选项。首先发布配置

php artisan vendor:publish --provider="safaeean\Sitemap\SitemapServiceProvider" --tag=config

这将默认配置复制到config/sitemap.php,您可以在这里编辑它。

use GuzzleHttp\RequestOptions;

return [

    /*
     * These options will be passed to GuzzleHttp\Client when it is created.
     * For in-depth information on all options see the Guzzle docs:
     *
     * http://docs.guzzlephp.org/en/stable/request-options.html
     */
    'guzzle_options' => [

        /*
         * Whether or not cookies are used in a request.
         */
        RequestOptions::COOKIES => true,

        /*
         * The number of seconds to wait while trying to connect to a server.
         * Use 0 to wait indefinitely.
         */
        RequestOptions::CONNECT_TIMEOUT => 10,

        /*
         * The timeout of the request in seconds. Use 0 to wait indefinitely.
         */
        RequestOptions::TIMEOUT => 10,

        /*
         * Describes the redirect behavior of a request.
         */
        RequestOptions::ALLOW_REDIRECTS => false,
    ]

];

用法

生成网站地图

最简单的方法是爬取指定域名并生成包含所有找到的链接的网站地图。网站地图的目的是通过$path指定。

SitemapGenerator::create('https://example.com')->writeToFile($path);

生成的网站地图看起来类似于这个

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>https://example.com</loc>
        <lastmod>2016-01-01T00:00:00+00:00</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>
    <url>
        <loc>https://example.com/page</loc>
        <lastmod>2016-01-01T00:00:00+00:00</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>

    ...
</urlset>

自定义网站地图生成器

更改属性

要更改联系页面的lastmodchangefreqpriority

use Carbon\Carbon;
use safaeean\Sitemap\SitemapGenerator;
use safaeean\Sitemap\Tags\Url;

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

       return $url;
   })
   ->writeToFile($sitemapPath);

省略一些链接

如果您不希望爬取的链接出现在网站地图中,只需在传递给hasCrawled的可调用函数中不返回它即可。

use safaeean\Sitemap\SitemapGenerator;
use safaeean\Sitemap\Tags\Url;

SitemapGenerator::create('https://example.com')
   ->hasCrawled(function (Url $url) {
       if ($url->segment(1) === 'contact') {
           return;
       }

       return $url;
   })
   ->writeToFile($sitemapPath);

防止爬虫爬取一些页面

您也可以通过传递一个callableshouldCrawl来指示基础爬虫不爬取一些页面。

use safaeean\Sitemap\SitemapGenerator;
use safaeean\Crawler\Url;

SitemapGenerator::create('https://example.com')
   ->shouldCrawl(function (Url $url) {
       // All pages will be crawled, except the contact page.
       // Links present on the contact page won't be added to the
       // sitemap unless they are present on a crawlable page.
       return $url->segment(1) !== 'contact';
   })
   ->writeToFile($sitemapPath);

手动添加链接

您可以手动将链接添加到网站地图中

use safaeean\Sitemap\SitemapGenerator;
use safaeean\Sitemap\Tags\Url;

SitemapGenerator::create('https://example.com')
    ->getSitemap()
    // here we add one extra link, but you can add as many as you'd like
    ->add(Url::create('/extra-page')->setPriority(0.5))
    ->writeToFile($sitemapPath);

添加链接的替代项

多语言网站可能有同一页面的多个替代版本(每个语言一个)。基于前面的示例,添加替代项可以这样做

use safaeean\Sitemap\SitemapGenerator;
use safaeean\Sitemap\Tags\Url;

SitemapGenerator::create('https://example.com')
    ->getSitemap()
    // here we add one extra link, but you can add as many as you'd like
    ->add(Url::create('/extra-page')->setPriority(0.5)->addAlternate('/extra-pagina', 'nl'))
    ->writeToFile($sitemapPath);

注意addAlternate函数,它接受一个替代URL和它所属的语言。

手动创建网站地图

您也可以完全手动创建网站地图

use Carbon\Carbon;

Sitemap::create()
   ->add('/page1')
   ->add('/page2')
   ->add(Url::create('/page3')->setLastModificationDate(Carbon::create('2016', '1', '1')))
   ->writeToFile($sitemapPath);

创建网站地图索引

您可以创建网站地图索引

use safaeean\Sitemap\SitemapIndex;

SitemapIndex::create()
    ->add('/pages_sitemap.xml')
    ->add('/posts_sitemap.xml')
    ->writeToFile($sitemapIndexPath);

您可以传递一个safaeean\Sitemap\Tags\Sitemap对象来手动设置lastModificationDate属性。

use safaeean\Sitemap\SitemapIndex;
use safaeean\Sitemap\Tags\Sitemap;

SitemapIndex::create()
    ->add('/pages_sitemap.xml')
    ->add(Sitemap::create('/posts_sitemap.xml')
        ->setLastModificationDate(Carbon::yesterday()))
    ->writeToFile($sitemapIndexPath);

生成的网站地图索引将类似于这个

<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
   <sitemap>
      <loc>http://www.example.com/pages_sitemap.xml</loc>
      <lastmod>2016-01-01T00:00:00+00:00</lastmod>
   </sitemap>
   <sitemap>
      <loc>http://www.example.com/posts_sitemap.xml</loc>
      <lastmod>2015-12-31T00:00:00+00:00</lastmod>
   </sitemap>
</sitemapindex>

频繁生成网站地图

您的网站可能会不时更新。为了使您的网站地图反映这些更改,您可以定期运行生成器。最简单的方法是利用Laravel的默认调度功能。

您可以设置一个类似于以下这样的Artisan命令

namespace App\Console\Commands;

use Illuminate\Console\Command;
use safaeean\Sitemap\SitemapGenerator;

class GenerateSitemap extends Command
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $signature = 'sitemap:generate';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Generate the sitemap.';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // modify this to your own needs
        SitemapGenerator::create(config('app.url'))
            ->writeToFile(public_path('sitemap.xml'));
    }
}

然后应在控制台内核中安排此命令。

// app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
    ...
    $schedule->command('sitemap:generate')->daily();
    ...
}

变更日志

请参阅变更日志以获取有关最近更改的更多信息。

测试

首先在一个单独的终端会话中启动测试服务器

cd tests/server
./start_server.sh

服务器运行时,您可以执行测试

$ composer test

贡献

请参阅CONTRIBUTING获取详细信息。

安全性

如果您发现任何与安全相关的问题,请通过电子邮件freek@mndco.ir联系,而不是使用问题跟踪器。

致谢

关于safaeean

safaeean是一家位于比利时安特卫普的网页设计公司。您可以在我们的网站上找到我们所有开源项目的概览在这里

许可

MIT许可证(MIT)。请参阅许可文件获取更多信息。