oihana / php-robots
The Oihana PHP Robots library
Requires
- php: >=8.4
- oihana/php-commands: dev-main
- oihana/php-core: dev-main
- oihana/php-enums: dev-main
- oihana/php-exceptions: dev-main
- oihana/php-files: dev-main
- oihana/php-reflect: dev-main
- oihana/php-schema: dev-main
- oihana/php-signals: dev-main
- oihana/php-standards: dev-main
- oihana/php-system: dev-main
Requires (Dev)
- mikey179/vfsstream: ^1.6
- nunomaduro/collision: ^8.8
- phpdocumentor/shim: ^3.8
- phpunit/phpunit: ^12
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A PHP library to create and manage robots.txt files, built on top of the oihana-php-commands library.
✨ Features
- Create or remove a project's robots.txt from the CLI
- Optional custom path via -f|--file (absolute or relative)
- Clear console support via -c|--clear
- Config-driven defaults (config.toml)
- Read the groups of an existing robots.txt, and add, change or remove them while every other byte of the file stays as it was (since 1.1.0)
A robots.txt file is a simple text file placed on a website’s root directory to give instructions to web crawlers (like search engine bots) about which pages or sections of the site should or shouldn’t be crawled and indexed. It helps control search engine access and manage site visibility.
📦 Installation
Requires PHP 8.4+
Install via Composer:
composer require oihana/php-robots
🚀 Usage
The package provides a Symfony Console command named command:robots with actions:
- create: Generate a
robots.txtfile - remove: Delete a
robots.txtfile
Examples:
Create a robots.txt using defaults
bin/console command:robots create
Create a robots.txt at a custom path
bin/console command:robots create --path /var/www/my-website/htdocs/robots.txt
or
bin/console command:robots create --p /var/www/my-website/htdocs/robots.txt
Remove the default robots.txt
bin/console command:robots remove
Remove a robots.txt at a custom path
bin/console command:robots remove --path /var/www/my-website/htdocs/robots.txt
Clear the console before running
bin/console command:robots create --clear
Options
| Option | ShortCut | Description |
|---|---|---|
| --clear | -c | Clear the console before running |
| --path | -p | The directory path of the 'robots.txt' file |
Notes:
- If
--pathis a relative path, it is resolved against the current working directory. - On creation/removal, parent directory existence and permissions are validated.
⚙️ Configuration
You can set defaults in config/config.toml under the [robots] section:
[robots] path = "/path/to/your/project/htdocs" overwrite = true permissions = 0o644 owner = "www-data" group = "www-data" content = ''' User-agent: * Disallow: / '''
🧪 Programmatic usage
You can instantiate and configure the command in PHP if needed:
use DI\Container; use oihana\robots\commands\RobotsCommand; $container = new Container(); $command = new RobotsCommand ( null, // let kernel resolve the name $container, [ 'robots' => [ 'path' => '/var/www/my-website/htdocs', 'content' => "User-agent: *\nDisallow: /private/" ] ] );
📝 Reading and editing an existing robots.txt
Since 1.1.0, the functions of oihana\robots\helpers read the groups of a robots.txt text and
change them. They take the text and give the new text back: reading and writing the file is the
caller's business.
Every other byte stays as it was: comments (those a plugin writes too), blank lines, the order,
the line endings (\n or \r\n, those of the file), a last line with or without its ending. New
lines are inserted into the text as it was read, never written out of a parsed model.
A group, as RFC 9309 defines it, is one or more User-agent lines that follow one another, then the
lines that apply to those robots: Allow, Disallow, and Crawl-delay, which many robots read. A
group is named by its place in the list robotsGroups() gives, from 0.
| Function | What it does |
|---|---|
robotsGroups( $text ) |
The groups: their robots, rules, Crawl-delay and line numbers |
robotsGroupsFor( $groups , $agent , $orStar = true ) |
The groups a robot follows: its own, or else the * groups |
robotsAddGroup( $text , $agents , $rules = [] , $crawlDelay = null ) |
Adds a group at the end |
robotsAddRules( $text , $group , $rules ) |
Adds rules after the group's last one, each once |
robotsRemoveRule( $text , $group , $field , $value ) |
Removes a rule's line |
robotsSetCrawlDelay( $text , $group , $delay ) |
Replaces the delay's value, or adds the line |
robotsRemoveCrawlDelay( $text , $group ) |
Removes the delay's line |
robotsRemoveGroups( $text , $groups ) |
Removes one or several groups in one call |
robotsStarRules( $groups ) |
Every rule of every * group, each once |
robotsMissingStarRules( $groups , $agent ) |
The * rules a robot's own groups do not carry |
robotsChangePlan( $text , $agent , $rules , $crawlDelay = null ) |
Creates or completes a robot's group, the * rules copied in first |
robotsRuleMatches( $value , $target ) |
Whether a rule reaches an address, * and $ read as RFC 9309 says |
robotsPatternQueryName( $value ) |
The parameter a rule reaches right after the ? |
And the pieces they are made of: robotsLines(), robotsField(), robotsRuleLine(),
robotsCrawlDelayLine(), robotsCrawlDelayLines(), robotsInsertLines(), robotsRemoveLines().
Something that is not there is refused with an InvalidArgumentException, never written or removed
in silence: a group, a rule or a delay that does not exist, a rule value with a line break or a #.
The trap of a group for one robot. A robot that finds a group of its own ignores every *
group. Its group must carry the general rules again, from every * group, a plugin's included.
robotsChangePlan() does it: a new group gets every * rule first, an existing one the * rules
it lacks, then the rules asked for, each once, its Crawl-delay kept unless one is given:
use function oihana\robots\helpers\robotsChangePlan; $text = file_get_contents( '/var/www/my-website/htdocs/robots.txt' ) ; $plan = robotsChangePlan( $text , 'ExampleBot' , [ [ 'field' => 'disallow' , 'value' => '/wp-json/' ] ] , '10' ) ; $plan[ 'copied' ] ; // the * rules written into the group $plan[ 'already' ] ; // the rules asked for it already had $text = $plan[ 'text' ] ;
It refuses *, and a robot whose every group is shared with other robots: a rule added would
reach them too.
What a rule reaches. robotsRuleMatches() compares a rule with an address as RFC 9309 does: the
start of the path and query, * for any run of characters, $ at the end for the end of the
address. A pattern on a parameter, /*?session=, reaches it only right after the ?:
?page=2&session=3 escapes it, and /*&session= is the rule for that one.
robotsPatternQueryName( '/*?session=' ) gives session, to look for what escapes.
Removing several groups: one call. Removing group 2 brings the next ones up. Give every place,
read once, to robotsRemoveGroups(); to remove every group of a robot, give it what
robotsGroupsFor( $groups , 'ExampleBot' , false ) finds.
🔚 Exit codes
- 0 Success
- 1 Failure (invalid action, IO failure, etc.)
❓ Troubleshooting
- Ensure the parent directory for the
robots.txtis writable (especially when using a custom--pathoption). - When using relative paths with
--path, they are resolved from the current working directory (pwd). - For more details, see the inline documentation in RobotsCommand.php.
✅ Running Unit Tests
To run all tests:
composer test
🧾 License
This project is licensed under the Mozilla Public License 2.0 (MPL-2.0).
👤 About the author
- Author: Marc ALCARAZ (aka eKameleon)
- Mail: marc@ooop.fr
- Website: http://www.ooop.fr
