middlewares / robots
Middleware to enable/disable the robots of the search engines
Installs: 8 955
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: ^7.2 || ^8.0
- middlewares/utils: ^3.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- laminas/laminas-diactoros: ^2.3
- oscarotero/php-cs-fixer-config: ^1.0
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^8|^9
- squizlabs/php_codesniffer: ^3.0
README
Middleware to enable/disable the robots of the search engines for non-production environment. Adds automatically the header X-Robots-Tag
in all responses and returns a default body for /robots.txt
request.
Requirements
- PHP >= 7.2
- A PSR-7 http library
- A PSR-15 middleware dispatcher
Installation
This package is installable and autoloadable via Composer as middlewares/robots.
composer require middlewares/robots
Example
$dispatcher = new Dispatcher([ new Middlewares\Robots(false) ]); $response = $dispatcher->dispatch(new ServerRequest()); echo $response->getHeaderLine('X-Robots-Tag'); //noindex, nofollow, noarchive
Usage
The constructor's first argument configure whether block or not search engines.
//Disallow search engine robots $robots = new Middlewares\Robots(false); //Allow search engine robots $robots = new Middlewares\Robots(true);
Optionally, you can provide a Psr\Http\Message\ResponseFactoryInterface
as the second argument to create the response of the requests to /robots.txt
. If it's not defined, Middleware\Utils\Factory will be used to detect it automatically.
$responseFactory = new MyOwnResponseFactory(); $robots = new Middlewares\Robots(false, $responseFactory);
sitemap
If your site has a sitemap, use this option to add the url to robots.txt
responses.
$robots = (new Middlewares\Robots(true))->sitemap('/sitemap.xml');
Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.
The MIT License (MIT). Please see LICENSE for more information.