https://github.com/tekord/robots-txt-provider
Provides various framework-agnostic ways to generate the contents of the robots.txt file
https://github.com/tekord/robots-txt-provider
content-generation framework-agnostic php robots-txt seo seo-optimization
Last synced: 21 days ago
JSON representation
Provides various framework-agnostic ways to generate the contents of the robots.txt file
- Host: GitHub
- URL: https://github.com/tekord/robots-txt-provider
- Owner: tekord
- License: mit
- Created: 2021-06-06T17:00:38.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-06-06T17:39:20.000Z (over 4 years ago)
- Last Synced: 2025-03-11T04:49:16.882Z (8 months ago)
- Topics: content-generation, framework-agnostic, php, robots-txt, seo, seo-optimization
- Language: PHP
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Framework-agnostic ROBOTS.TXT File Content Generator Kit
This package provides various framework-agnostic ways to generate the contents of the robots.txt file.
## Installation
Install the package via Composer:
```bash
composer require tekord/robots-txt-provider
```
## Usage
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
### ContentBuilder
The ContentBuilder class provides handy methods for generating content:
``` php
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:
```php
$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: /
```
### StringContentProvider
```php
$content = <<<'TXT'
User-Agent: *
Disallow: /
TXT;
$contentProvider = new StringContentProvider($content);
```
### FileContentProvider
```php
$contentProvider = new FileContentProvider(__DIR__ . "/storage/static/default-robots.txt");
```
### CallbackContentProvider
```php
$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();
});
```
### CompositeContentProvider
```php
$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);
```
## Testing
```bash
composer test
```
## Security
If you discover any security related issues, please email [cyrill@tekord.space](mailto:cyrill@tekord.space) instead of
using the issue tracker.