verschuur/laravel-robotstxt 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

verschuur/laravel-robotstxt

Composer 安装命令:

composer require verschuur/laravel-robotstxt

包简介

Set the robots.txt content dynamically based on the Laravel app environment.

README 文档

README

Run tests Code Climate issues Code Climate maintainability Scrutinizer

Dynamic robots.txt ServiceProvider for Laravel 🤖

Installation

Composer

composer require verschuur/laravel-robotstxt

Manual

Add the following to your composer.json and then run composer install.

{
    "require": {
        "verschuur/laravel-robotstxt": "^3.0"
    }
}

Service provider registration

This package supports Laravel's service provider autodiscovery so that's it. If you wish to register the package manually, add the ServiceProvider to the providers array in config/app.php.

Verschuur\Laravel\RobotsTxt\Providers\RobotsTxtProvider::class

Usage

Basic usage

This package adds a /robots.txt route to your application. Remember to remove the physical robots.txt file from your /public dir or else it will take precedence over Laravel's route and this package will not work.

By default, the production environment will show

User-agent: *
Disallow:

while every other environment will show

User-agent: *
Disallow: /

This will allow the default install to allow all robots on a production environment, while disallowing robots on every other environment.

Custom settings

If you need custom sitemap entries, publish the configuration file

php artisan vendor:publish --provider="Verschuur\Laravel\RobotsTxt\Providers\RobotsTxtProvider"

This will copy the robots-txt.php config file to your app's config folder. In this file you will find the following array structure

'environments' => [
    '{environment name}' => [
        'paths' => [
            '{robot name}' => [
                'disallow' => [
                    ''
                ],
                'allow' => []
            ],
        ]
    ]
]

In which:

  • {environment name}: the enviroment for which to define the paths.
  • {robot name}: the robot for which to define the paths.
  • disallow: all entries which will be used by the disallow directive.
  • allow: all entries which will be used by the allow directive.

By default, the environment name is set to production with a robot name of * and a disallow entry consisting of an empty string. This will allow all bots to access all paths on the production environment.

Note: If you do not define any environments in this configuration file (i.e. an empty configuration), the default will always be to disallow all bots for all paths.

Examples

For brevity, the environment array key will be disregarded in these examples.

Allow all paths for all robots on production, and disallow all paths for every robot in staging.

'production' => [
    'paths' => [
        '*' => [
            'disallow' => [
                ''
            ]
        ]
    ]
],
'staging' => [
    'paths' => [
        '*' => [
            'disallow' => [
                '/'
            ]
        ]
    ]
]

Allow all paths for all robot bender on production, but disallow /admin and /images on production for robot flexo

'production' => [
    'paths' => [
        'bender' => [
            'disallow' => [
                ''
            ]
        ],
        'flexo' => [
            'disallow' => [
                '/admin',
                '/images'
            ]
        ]
    ]
],

Allow directive

Besides the more standard disallow directive, the allow directive is also supported.

Allow a path, but disallow sub paths:

'production' => [
    'paths' => [
        '*' => [
            'disallow' => [
                '/foo/bar'
            ],
            'allow' => [
                '/foo'
            ]
        ]
    ]
],

When the file is rendered, the disallow directives will always be placed before the allow directives.

If you don't need one or the other directive, and you wish to keep the configuration file clean, you can simply remove the entire key from the entire array.

Sitemaps

This package also allows to add sitemaps to the robots file. By default, the production environment will add a sitemap.xml entry to the file. You can remove this default entry from the sitemaps array if you don't need it.

Because sitemaps always need to an absolute url, they are automatically wrapped using Laravel's url() helper function. The sitemap entries in the config file should be relative to the webroot.

The standard production configuration

'environments' => [
    'production' => [
        'sitemaps' => [
            'sitemap.xml'
        ]
    ]
]

Adding multiple sitemaps

'environments' => [
    'production' => [
        'sitemaps' => [
            'sitemap-articles.xml',
            'sitemap-products.xml',
            'sitemap-etcetera.xml'
        ]
    ]
]

Compatiblility

This package is compatible with Laravel 12 and 13. For a complete overview of supported Laravel and PHP versions, please refer to the 'Run test' workflow.

Testing

PHPUnit test cases are provided in /tests. Run the tests through composer run test or vendor/bin/phpunit --configuration phpunit.xml.

robots.txt reference

The following reference was while creating this package:

https://developers.google.com/search/reference/robots_txt

verschuur/laravel-robotstxt 适用场景与选型建议

verschuur/laravel-robotstxt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 428.83k 次下载、GitHub Stars 达 39, 最近一次更新时间为 2017 年 01 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「robots.txt」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 verschuur/laravel-robotstxt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 verschuur/laravel-robotstxt 我们能提供哪些服务?
定制开发 / 二次开发

基于 verschuur/laravel-robotstxt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 428.83k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 43
  • 点击次数: 16
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 39
  • Watchers: 4
  • Forks: 14
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-01-16