This package provides various framework-agnostic ways to generate the contents of the robots.txt file.
Install the package via Composer:
composer require tekord/robots-txt-provider
Here are the available content providers:
- StringContentProvider - returns a plain string
- FileContentProvider - returns the contents of the specified file
- CallbackContentProvider - used a callback to get the value
- CompositeContentProvider - used to compose multiple providers, returns the value of the first provider that returns a non-NULL value
The ContentBuilder class provides handy methods for generating content:
ContentBuilder::make()
->line("User-Agent: *")
->emptyLine()
->comment("This is a comment")
->parameter("Host", "https://example.com")
->allow("/about")
->disallow("/login")
->build();
Will return the following:
User-Agent: *
# This is a comment
Host: https://example.com
Allow: /about
Disallow: /login
The following methods have a conditional version: line
, emptyLine
, comment
, parameter
, allow
and disallow
.
Just add If
to the end of the method name like lineIf
. The first parameter is a condition (boolean or callback). If
the condition is TRUE then the method executes. For instance:
$debugMode = true;
$useSpecialUserAgent = function () {
return false;
};
ContentBuilder::make()
->lineIf($useSpecialUserAgent, "User-Agent: Bot/1.0")
->lineIf(!$useSpecialUserAgent, "User-Agent: *")
->emptyLine()
->commentIf($debugMode, "Debug mode is active")
->parameter("Host", "https://example.com")
->disallow("/")
->build();
Will return the following:
User-Agent: *
# Debug mode is active
Host: https://example.com
Disallow: /
$content = <<<'TXT'
User-Agent: *
Disallow: /
TXT;
$contentProvider = new StringContentProvider($content);
$contentProvider = new FileContentProvider(__DIR__ . "/storage/static/default-robots.txt");
$contentProvider = new CallbackContentProvider(function() {
return ContentBuilder::make()
->line("User-Agent: *")
->emptyLine()
->comment("This content was generated by the CallbackContentProvider class")
->parameter("Host", "https://example.com")
->disallow("/")
->build();
});
$productionFileContentProvider = new FileContentProvider(__DIR__ . "/public/robots.production.txt");
$defaultFileContentProvider = new FileContentProvider(__DIR__ . "/public/robots.default.txt");
$fallbackFileContentProvider = new StringContentProvider("User-Agent: *\nDisallow: /");
$compositeContentProvider = (new CompositeContentProvider())
->addContentProvider($productionFileContentProvider)
->addContentProvider($defaultFileContentProvider)
->addContentProvider($fallbackFileContentProvider);
composer test
If you discover any security related issues, please email cyrill@tekord.space instead of using the issue tracker.