StackPHP middleware providing robots.txt disallow for non-production environments
Switch branches/tags
Nothing to show
Clone or download
Phil Sturgeon
Phil Sturgeon Ninja 5.6 test
Latest commit 0a48d0f Feb 11, 2014
Type Name Latest commit message Commit time
Failed to load latest commit information.
src PSR-4 Feb 1, 2014
tests/League/StackRobots Add X-Robots-Tag support. Nov 2, 2013
.gitignore Initial Commit Nov 1, 2013
.travis.yml Ninja 5.6 test Feb 11, 2014
LICENSE Initial Commit Nov 1, 2013 Update README Nov 2, 2013
composer.json PSR-4 Feb 1, 2014
phpunit.xml.dist 100% Code Coverage Nov 1, 2013


Build Status Total Downloads Latest Stable Version

StackRobots is a middleware for StackPHP. It provides a default robots.txt for non-production environments.

Install Via Composer

    "require": {
        "league/stack-robots": "~1.0"


StackRobots is a very simple middleware. By default it looks at the SERVER_ENV environment variable, and if the SERVER_ENV does not equal production, it captures the response and sets an X-Robots-Tag header with a value of noindex, nofollow, noarchive.

When you push the middleware on to the stack, you can pass 2 additional parameters, $env and $envVar. The $env parameter is the environment in which you want this middleware to not do anything, typically production. The $envVar parameter is the environment variable that holds the environment of the current server; it defaults to SERVER_ENV.

If the value of SERVER_ENV matches the value that is passed, this middleware will just pass control on to the next middleware. However, if it does not match, then StackRobots will set the X-Robots-Tag. Additionally, if the incoming request is for your /robots.txt file, then StackRobots will stop the request and send the following response.

return new Response("User-Agent: *\nDisallow: /", 200, array('Content-Type' => 'text/plain'));

And this is what the browser receives.

User-Agent: *
Disallow: /

More info on the X-Robots-Tag is available here.


include_once '../vendor/autoload.php';

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use League\StackRobots\Robots;

$app = new Stack\CallableHttpKernel(function (Request $request) {
    return new Response('Hello World!');


$app = (new Stack\Builder)