oihana/php-robots

The Oihana PHP Robots library

dev-main 2025-08-11 16:56 UTC

This package is auto-updated.

Last update: 2025-08-11 16:56:14 UTC


README

Oihana PHP System

A PHP library to create and manage robots.txt files, built on top of the oihana-php-commands library.

Latest Version Total Downloads License

โœจ 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