league / stack-robots
StackRobots Middleware for StackPHP
Installs: 4 755
Dependents: 0
Suggesters: 0
Security: 0
Stars: 67
Watchers: 10
Forks: 5
Open Issues: 0
Requires
- php: >=5.3.10
- symfony/http-foundation: ~2.1
- symfony/http-kernel: ~2.1
Requires (Dev)
- stack/builder: 1.0.*@dev
- stack/callable-http-kernel: ~1.0@dev
- stack/run: ~1.0@dev
This package is not auto-updated.
Last update: 2023-05-22 18:41:41 UTC
README
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" } }
Usage
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.
Example
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!'); }); putenv('SERVER_ENV=dev'); $app = (new Stack\Builder) ->push('League\\StackRobots\\Robots') ->resolve($app); Stack\run($app);
Authors
- Don Gilbert @dilbert4life
- Inspired by Cylon for Ruby.