seophp/robots

Lightweight, framework-agnostic robots.txt management for PHP

Maintainers

Package info

github.com/seo-php/robots

pkg:composer/seophp/robots

Fund package maintenance!

dllobell

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

dev-main 2026-03-01 16:37 UTC

This package is auto-updated.

Last update: 2026-03-03 11:39:56 UTC


README

Total Downloads Latest Stable Version License PHP Minimum Version

Lightweight, framework-agnostic robots.txt management for PHP.

Requirements

  • PHP 8.4 or higher

Installation

Install via Composer:

composer require seophp/robots

Usage

You may create an instance of the main RobotsTxt class by hand:

use Seo\Robots\RobotsTxt;
use Seo\Robots\RobotsTxtGroup;
use Seo\Robots\RobotsTxtDirective;

$robots = new RobotsTxt(
    groups: [
        new RobotsTxtGroup(
            agents: ['*'],
            directives: [
                new RobotsTxtDirective('Disallow', '/admin'),
            ],
        ),
    ],
    sitemaps: [
        'https://example.com/sitemap.xml',
    ],
);

Using the builder

You may use the RobotsTxtBuilder to construct a RobotsTxt instance with a fluent API:

use Seo\Robots\RobotsTxtBuilder;
use Seo\Robots\RobotsTxtGroupBuilder;

$robots = (new RobotsTxtBuilder())
    ->group(fn (RobotsTxtGroupBuilder $g) => $g
        ->agent('*')
        ->disallow('/admin')
        ->allow('/admin/public')
        ->crawlDelay(10)
    )
    ->group(fn (RobotsTxtGroupBuilder $g) => $g
        ->agent('Googlebot')
        ->agent('Bingbot')
        ->disallow('/')
    )
    ->sitemap('https://example.com/sitemap.xml')
    ->build();

Rendering

You may use the RobotsTxtRenderer to convert a RobotsTxt instance to a string:

use Seo\Robots\RobotsTxtRenderer;

$renderer = new RobotsTxtRenderer();

$output = $renderer->render($robots);

Output:

User-agent: *
Disallow: /admin
Allow: /admin/public
Crawl-delay: 10

User-agent: Googlebot
User-agent: Bingbot
Disallow: /

Sitemap: https://example.com/sitemap.xml

Parsing

You may use the RobotsTxtParser to parse a robots.txt string into a RobotsTxt instance:

use Seo\Robots\RobotsTxtParser;

$parser = new RobotsTxtParser();

$robots = $parser->parse($content);

Note

  • Uses blank lines as group separators
  • Preserves original directive casing
  • Strips comments (# and everything after)
  • Throws RobotsTxtParseException on malformed lines or directives without a preceding User-agent

Querying

The RobotsTxt instance provides methods to check crawling permissions following RFC 9309 semantics:

$robots->isAllowed('Googlebot', '/public');    // true
$robots->isDisallowed('Googlebot', '/admin');  // true
$robots->crawlDelayFor('Googlebot');           // int or null

Matching rules

  • Agent resolution: case-insensitive prefix matching. "Googlebot-News" matches a "Googlebot" group. The most specific (longest) matching agent wins, with * as fallback.
  • Path matching: longest matching directive wins. Supports * (wildcard) and $ (end anchor) in patterns.
  • Conflict resolution: when Allow and Disallow patterns have equal length, Allow wins.
  • Default: paths are allowed when no directive matches.

License

The MIT License (MIT). See the license file for more information.