tourze/robots-txt-bundle
Composer 安装命令:
composer require tourze/robots-txt-bundle
包简介
robots.txt管理
README 文档
README
A Symfony Bundle for managing robots.txt files with a multi-provider architecture,
allowing flexible assembly and generation of robots.txt content.
Table of Contents
- Features
- Installation
- Requirements
- Configuration
- Usage
- API Reference
- Advanced Usage
- Testing
- Contributing
- License
Features
- 🤖 Complete robots.txt support: Support for all standard directives (User-agent, Allow, Disallow, Crawl-delay, Sitemap, etc.)
- 🔌 Multi-provider architecture: Collect rules from multiple sources by
implementing
RobotsTxtProviderInterface - 🎯 Priority control: Support provider priority to flexibly control rule merging order
- 🌍 Environment support: Provide different robots.txt content based on different environments
- ⚡ Performance optimization: Built-in caching strategy to reduce server load
- 📖 Standards compliant: Follows Google robots.txt specification
- 🔧 Auto-configuration: Providers are automatically registered using Symfony's autoconfiguration
Installation
composer require tourze/robots-txt-bundle
Requirements
- PHP 8.1 or higher
- Symfony 6.4 or higher
Configuration
The Bundle works out of the box with minimal configuration. The /robots.txt
route is automatically registered and will collect content from all registered
providers.
Custom Configuration
If you need to customize the Bundle behavior, create a configuration file:
# config/packages/robots_txt.yaml robots_txt: cache_enabled: true cache_ttl: 3600
Usage
Basic Usage
The Bundle automatically registers a /robots.txt route that returns the generated robots.txt content.
Creating Custom Providers
<?php namespace App\Provider; use Tourze\RobotsTxtBundle\Provider\RobotsTxtProviderInterface; use Tourze\RobotsTxtBundle\Model\RobotsTxtEntry; use Tourze\RobotsTxtBundle\Model\RobotsTxtRule; use Tourze\RobotsTxtBundle\Model\RobotsTxtDirective; class CustomRobotsTxtProvider implements RobotsTxtProviderInterface { public function provide(): RobotsTxtEntry { $entry = new RobotsTxtEntry(); // Add comments $entry = $entry->withComment('Custom robots.txt rules'); // Disallow all crawlers from accessing admin areas $adminRule = RobotsTxtRule::forAllAgents([ RobotsTxtDirective::disallow('/admin/'), RobotsTxtDirective::disallow('/private/'), ]); $entry = $entry->withRule($adminRule); // Set special rules for Googlebot $googlebotRule = RobotsTxtRule::forAgent('Googlebot', [ RobotsTxtDirective::allow('/api/public/'), RobotsTxtDirective::crawlDelay(1), ]); $entry = $entry->withRule($googlebotRule); // Add sitemap $entry = $entry->withSitemap('https://example.com/sitemap.xml'); return $entry; } public function getPriority(): int { return 100; // High priority } public function supports(): bool { // Only enable in production environment return ($_ENV['APP_ENV'] ?? 'prod') === 'prod'; } }
Providers are automatically registered without additional configuration.
API Reference
RobotsTxtDirective
Create individual robots.txt directives:
// Disallow directive RobotsTxtDirective::disallow('/admin/'); // Allow directive RobotsTxtDirective::allow('/public/'); // Crawl-delay directive RobotsTxtDirective::crawlDelay(10); // Sitemap directive RobotsTxtDirective::sitemap('https://example.com/sitemap.xml'); // Custom directive new RobotsTxtDirective('Custom-directive', 'value');
RobotsTxtRule
Create rule groups for specific User-agents:
// For all crawlers RobotsTxtRule::forAllAgents([ RobotsTxtDirective::disallow('/admin/'), ]); // For specific crawlers RobotsTxtRule::forAgent('Googlebot', [ RobotsTxtDirective::allow('/api/'), RobotsTxtDirective::crawlDelay(1), ], 100); // Priority
RobotsTxtEntry
Complete robots.txt entry:
$entry = new RobotsTxtEntry(); $entry = $entry->withComment('Generated robots.txt') ->withRule($rule) ->withSitemap('https://example.com/sitemap.xml');
Advanced Usage
Dynamic Rules
class DynamicRobotsTxtProvider implements RobotsTxtProviderInterface { public function __construct( private UserRepository $userRepository ) {} public function provide(): RobotsTxtEntry { $entry = new RobotsTxtEntry(); // Dynamically generate rules based on user data $users = $this->userRepository->findPublicUsers(); foreach ($users as $user) { $entry = $entry->withRule( RobotsTxtRule::forAllAgents([ RobotsTxtDirective::allow("/users/{$user->getId()}/") ]) ); } return $entry; } }
Conditional Providers
class ConditionalRobotsTxtProvider implements RobotsTxtProviderInterface { public function supports(): bool { // Only enable under specific conditions return $_ENV['ENABLE_SEO'] === 'true' && $_ENV['APP_ENV'] === 'prod'; } }
Testing
./vendor/bin/phpunit packages/robots-txt-bundle/tests
Contributing
Please see CONTRIBUTING.md for details on how to contribute to this project.
License
MIT License - Please see LICENSE file for more information.
tourze/robots-txt-bundle 适用场景与选型建议
tourze/robots-txt-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 06 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 tourze/robots-txt-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tourze/robots-txt-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-06-01