Install the Botmotive guard in Laravel
This page helps you add botmotive/laravel to a Laravel app, so it applies the fixes you choose in Botmotive.
The middleware enforces your rules: blocks, throttles, allow rules and path denials. It also serves Botmotive's robots.txt section and, when the RSL license is on, /license.xml. It also sends a compact record of each request to Botmotive, so you don't need another log source. If you already send logs from Railway, Cloudflare, Fastly or your own server, set BOTMOTIVE_LOGS=off so requests aren't counted twice.
What you need first
- A Botmotive account and a site. See Getting started.
- Your site ID, such as
my-shop. It is shown in Settings next to Site ID. - A token for the guard, which starts with
bm_. It needs both Fetch blocking rules and Send traffic logs. - Laravel 10, 11 or 12, and PHP 8.1 or later.
A new site starts with no token. Its install page makes one for you: open your site, choose Laravel (or click Connection steps in Settings), and click Make a token. The token is shown only once, and Botmotive keeps only a hash of it. You can also make one in Settings:
- Open your site in the dashboard and click Settings.
- Under Tokens, click New token. Name it after what will use it, such as
Laravel app, and click Make token. - Click Copy and store the token somewhere safe.
Other tokens keep working. If an old token may have leaked, click Revoke next to it.
Install
Install the package. Laravel finds its service provider on its own.
composer require botmotive/laravel:^0.2.0Add your site ID and token to
.env:BOTMOTIVE_SITE=my-shop BOTMOTIVE_TOKEN=bm_...Without both, the middleware does nothing.
Serve robots.txt from Laravel. Add this line to
routes/web.php:\Botmotive\Laravel\Botmotive::routes();Delete
public/robots.txt. The web server serves that file before Laravel sees the request.Optional: publish the config file to change the refresh interval or your robots.txt lines.
php artisan vendor:publish --tag=botmotive-configThis creates
config/botmotive.php.If you cache config, run
php artisan config:clearorphp artisan config:cacheagain.
The middleware is added to every request by default. It runs after TrustProxies, so it sees the client IP that TrustProxies works out.
Settings that matter
| Config key | Environment variable | Default |
|---|---|---|
site, token |
BOTMOTIVE_SITE, BOTMOTIVE_TOKEN |
none |
api |
BOTMOTIVE_API |
https://api.botmotive.io |
ip_header |
BOTMOTIVE_IP_HEADER |
none, so $request->ip() is used |
global |
BOTMOTIVE_GLOBAL |
true |
cache |
BOTMOTIVE_CACHE_STORE |
your default cache store |
refresh |
none | 60 seconds |
robots |
none | "User-agent: *\nAllow: /" |
logs |
BOTMOTIVE_LOGS (off, 0, false or no) |
on |
Logs. After each response has gone, the middleware writes a short record of the request (method, path, status, user agent, IP, timing and header names) to storage/botmotive. The scheduler runs php artisan botmotive:flush every minute to send them in gzip batches, so keep schedule:run in your cron. It never sends cookies, the Authorization header or request bodies, and secret-looking query values, such as token or password, become redacted. If Botmotive can't be reached, up to 20 MB of records wait on disk and the oldest go first. Set BOTMOTIVE_LOGS=off when another source already sends this site's logs.
Client IP. The middleware uses $request->ip(). Behind a load balancer, configure TrustProxies so that this is the visitor's address. In Laravel 11 and 12 that is in bootstrap/app.php:
->withMiddleware(function (Middleware $middleware) {
$middleware->trustProxies(at: '*');
})
In Laravel 10, set $proxies in app/Http/Middleware/TrustProxies.php. Trust only proxies you run.
Or set BOTMOTIVE_IP_HEADER to a header your edge sets: x-real-ip, cf-connecting-ip, x-forwarded-for, fly-client-ip or true-client-ip. The header is trusted as-is, so pick one visitors can't set. The one exception is cf-connecting-ip, which counts only when REMOTE_ADDR is a Cloudflare range or a private address. With x-forwarded-for, the middleware takes the rightmost public address. If the header holds no valid address, it falls back to $request->ip().
Cache across several servers. The rules feed and throttle counters live in Laravel's cache. When you run more than one server, use a shared store:
BOTMOTIVE_CACHE_STORE=redis
The database and memcached stores work too. With a file store, each server counts separately. Don't use the array store: it keeps nothing between requests.
Choosing routes. To guard only some routes, set BOTMOTIVE_GLOBAL=false. Then add the botmotive middleware alias where you want it:
Route::middleware('botmotive')->group(function () {
// routes to guard
});
robots.txt. Botmotive::routes() serves your lines from config('botmotive.robots') first. Botmotive's section follows, between # BEGIN Botmotive and # END Botmotive. When the RSL license is on, the section starts with a License: line.
/license.xml. When Serve an RSL license at /license.xml is ticked in Settings, the middleware answers /license.xml.
Domain. In Settings, fill in Domain under Your site's address, then click Save. Botmotive then reads your robots.txt and sitemap once a day.
Check it worked
Run the check on your server:
php artisan botmotive:checkIt prints
PASSorFAILfor the token, the API, your rules and a test log record, and exits with an error if any step fails. Within a minute, your site's page in the dashboard shows Guard connected: Laravel, last seen just now.Open
https://example.com/robots.txt. You should see your own lines. If you have chosen AI bot rules in Settings, you also see the# BEGIN Botmotivesection.Check traffic is arriving. Open your site in the dashboard and click the 15m tab.
Once requests arrive, the dashboard says Receiving data with a count of requests so far. Results follow 15 to 30 minutes after the first request, and the dashboard opens on the tab that has them.
Apply a block fix to a bot in the dashboard. Within a minute or so, request your site with that bot's user agent:
curl -i -A 'ExampleBot/1.0' https://example.com/You should see
403andForbidden.. A throttled visitor gets429with aRetry-Afterheader.
Updating and removing
- To update, run
composer update botmotive/laravel. - To use a new token, change
BOTMOTIVE_TOKENin.env. Clear or rebuild the config cache if you use one. - To remove it, delete the
Botmotive::routes()line and runcomposer remove botmotive/laravel. Deleteconfig/botmotive.phpif you published it. Put backpublic/robots.txtif you need one.
If something goes wrong
The middleware fails open. The feed refreshes every minute under a cache lock. If Botmotive can't be reached, the last rules stay in force. With no rules, every request goes through. Errors go to Laravel's log through report().
botmotive rules: HTTP 401in the log. The site ID or token is wrong, or the token was replaced.- robots.txt has no Botmotive section. Check that
public/robots.txtis gone, and that you have chosen AI bot rules or opt-outs. - IP rules don't match. Configure TrustProxies or set
BOTMOTIVE_IP_HEADER.
For more, see Troubleshooting.