oihana / php-robots
The Oihana PHP Robots library
dev-main
2025-08-11 16:56 UTC
Requires
- php: >=8.4
- oihana/php-commands: dev-main
- oihana/php-core: dev-main
- oihana/php-files: dev-main
- oihana/php-schema: 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
This package is auto-updated.
Last update: 2025-08-11 16:56:14 UTC
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)
๐ฆ 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.txt file
- remove: Delete a robots.txt file
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 --file /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 --file /var/www/my-website/htdocs/robots.txt
Clear the console before running
bin/console command:robots create --clear
Options
- -c, --clear Clear the console before running
- -f, --file=PATH Target robots.txt file path
Notes:
- If --file is 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] file = "/path/to/your/project/htdocs/robots.txt" 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' => [ 'file' => '/var/www/my-website/htdocs/robots.txt', 'content' => "User-agent: *\nDisallow: /private/" ] ] );
๐ Exit codes
- 0 Success
- 1 Failure (invalid action, IO failure, etc.)
โ Troubleshooting
- Ensure the parent directory for the robots.txt is writable (especially when using a custom --file path).
- When using relative paths with --file, 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