PHP code example of becklyn / robots-txt

1. Go to this page and download the library: Download becklyn/robots-txt library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

becklyn / robots-txt example snippets


use Becklyn\RobotsTxt\Builder\RobotsTxtBuilder;

$builder = new RobotsTxtBuilder();

// adding a section
$builder->getSection("google")
    ->allow("/public")
    ->disallow("/admin")
    ->crawlDelay(20);
    
$builder->getSection("bing")
    ->allow("/public")
    ->disallow("/admin")
    ->disallow("/private")
    ->crawlDelay(15);

$builder->getSection("google", "bing")
    ->allow("/public")
    ->disallow("/admin")
    ->crawlDelay(20);

$builder->getSection("google")
    ->allow("/public");
    
// ... some code ...

$builder->getSection("google")
    ->disallow("/admin")
    
// will produce a single entry:
//
//      User-Agent: google
//      Allow: /public
//      Disallow: /admin

$builder
    ->addSitemap("https://example.org/sitemap.xml.tar.gz")
    ->addSitemap("https://example.org/sitemap.xml");

$builder
    ->setHeader("This is some example text");
    
$builder->getSection("google")
    ->allow("/public");
    
// Will produce:
//
//      # This is some example text
//
//      User-Agent: google
//      Allow: /public

$content = $builder->getFormatted();
file_put_contents("robots.txt", $content);