becklyn/robots-txt

构建 robots.txt 的库

1.1.0 2022-02-18 08:17 UTC

This package is auto-updated.

Last update: 2024-09-18 13:45:16 UTC


README

用法

首先,您需要创建一个构建器。使用这个构建器,您可以创建不同用户代理的分区,并将指令添加到其中。

您还可以向 robots.txt 添加标题,并注册您的 sitemap URL。

添加分区

use Becklyn\RobotsTxt\Builder\RobotsTxtBuilder;

$builder = new RobotsTxtBuilder();

// adding a section
$builder->getSection("google")
    ->allow("/public")
    ->disallow("/admin")
    ->crawlDelay(20);
    
$builder->getSection("bing")
    ->allow("/public")
    ->disallow("/admin")
    ->disallow("/private")
    ->crawlDelay(15);

如果多个搜索引擎具有相同的指令,您可以为一个所有搜索引擎添加一个分区

$builder->getSection("google", "bing")
    ->allow("/public")
    ->disallow("/admin")
    ->crawlDelay(20);

如果可能,构建器会尝试将多个分区的指令打包在一起

$builder->getSection("google")
    ->allow("/public");
    
// ... some code ...

$builder->getSection("google")
    ->disallow("/admin")
    
// will produce a single entry:
//
//      User-Agent: google
//      Allow: /public
//      Disallow: /admin

网站地图

$builder
    ->addSitemap("https://example.org/sitemap.xml.tar.gz")
    ->addSitemap("https://example.org/sitemap.xml");

标题

您还可以添加一个标题,它将被包含在最顶部

$builder
    ->setHeader("This is some example text");
    
$builder->getSection("google")
    ->allow("/public");
    
// Will produce:
//
//      # This is some example text
//
//      User-Agent: google
//      Allow: /public

输出 robots.txt

$content = $builder->getFormatted();
file_put_contents("robots.txt", $content);