定制 seophp/robots-laravel 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

seophp/robots-laravel

Composer 安装命令:

composer require seophp/robots-laravel

包简介

README 文档

README

Total Downloads Latest Stable Version License PHP Minimum Version

Laravel integration for seophp/robots. Serves a dynamic robots.txt response via a service provider you define in your application.

Requirements

  • PHP 8.4 or higher
  • Laravel 12 or higher

Installation

Install via Composer:

composer require seophp/robots-laravel

Then run the install command to scaffold your service provider:

php artisan robots:install

This will:

  • Create app/Providers/RobotsTxtServiceProvider.php in your application
  • Register it automatically in bootstrap/providers.php

Configuration

All configuration lives inside the generated service provider. Implement the configure method, which receives a RobotsTxtConfig instance:

<?php

declare(strict_types=1);

namespace App\Providers;

use Seo\Robots\Laravel\Providers\RobotsTxtServiceProvider as ServiceProvider;
use Seo\Robots\Laravel\RobotsTxtConfig;
use Seo\Robots\RobotsTxtBuilder;
use Seo\Robots\RobotsTxtGroupBuilder;

final class RobotsTxtServiceProvider extends ServiceProvider
{
    protected function configure(RobotsTxtConfig $config): void
    {
        $config->robots(
            fn () => (new RobotsTxtBuilder())
                ->group(
                    fn (RobotsTxtGroupBuilder $group) => $group
                        ->agent('*')
                        ->disallow('/admin')
                        ->allow('/'),
                )
                ->sitemap('https://example.com/sitemap.xml')
                ->build(),
        );
    }
}

The route /robots.txt is registered automatically when the provider is loaded.

Environments

The robots closure is only called in production and local environments. In all other environments (e.g. staging), the package automatically serves a disallow-all response:

User-agent: *
Disallow: /

This prevents non-production deployments from leaking into search engine indexes without any extra configuration.

Middleware

Apply middleware to the /robots.txt route via middleware():

$config->middleware(['throttle:60,1']);

Domain constraints

Restrict the route to a specific domain or subdomain. This is useful in monorepo setups where multiple apps share the same Laravel installation but each needs its own robots.txt:

// App\Providers\MarketingRobotsTxtServiceProvider
$config
    ->domain('marketing.example.com')
    ->robots(fn () => (new RobotsTxtBuilder())
        ->group(fn (RobotsTxtGroupBuilder $group) => $group->agent('*')->allow('/'))
        ->build()
    );

// App\Providers\ApiRobotsTxtServiceProvider
$config
    ->domain('api.example.com')
    ->robots(fn () => (new RobotsTxtBuilder())
        ->group(fn (RobotsTxtGroupBuilder $group) => $group->agent('*')->disallow('/'))
        ->build()
    );

Register each as a separate provider in bootstrap/providers.php.

If domain() is not called, the config acts as a catch-all and matches any host. This is the correct setup for single-app projects and also works as a fallback in monorepo setups alongside domain-specific providers:

// App\Providers\ApiRobotsTxtServiceProvider — only for api.example.com
$config
    ->domain('api.example.com')
    ->robots(fn () => (new RobotsTxtBuilder())
        ->group(fn (RobotsTxtGroupBuilder $group) => $group->agent('*')->disallow('/'))
        ->build()
    );

// App\Providers\RobotsTxtServiceProvider — catch-all for all other domains
$config->robots(fn () => (new RobotsTxtBuilder())
    ->group(fn (RobotsTxtGroupBuilder $group) => $group->agent('*')->allow('/'))
    ->sitemap('https://example.com/sitemap.xml')
    ->build()
);

License

The MIT License (MIT). See the license file for more information.

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固