fuelviews/laravel-robots-txt
Composer 安装命令:
composer require fuelviews/laravel-robots-txt
包简介
Laravel robots txt package
README 文档
README
Laravel Robots.txt is a robust and easy-to-use solution designed to automatically generate and serve dynamic robots.txt files for your Laravel application. The package provides intelligent caching, environment-based rules, and seamless integration with your application's routing system.
Requirements
- PHP ^8.3
- Laravel ^10.0 || ^11.0 || ^12.0
Installation
Install the package via Composer:
composer require fuelviews/laravel-robots-txt
Publish the configuration file:
php artisan vendor:publish --tag="robots-txt-config"
Basic Usage
Automatic Route Registration
The package automatically registers a route at /robots.txt that serves your dynamic robots.txt file:
https://yoursite.com/robots.txt
Configuration
Configure your robots.txt rules in config/robots-txt.php:
<?php return [ /** * The disk where the robots.txt file will be saved */ 'disk' => 'public', /** * User agent rules for different paths */ 'user_agents' => [ '*' => [ 'Allow' => [ '/', ], 'Disallow' => [ '/admin', '/dashboard', ], ], 'Googlebot' => [ 'Allow' => [ '/', ], 'Disallow' => [ '/admin', ], ], ], /** * Sitemaps to include in robots.txt */ 'sitemap' => [ 'sitemap.xml', ], ];
Environment Behavior
Development/Staging Environments
In non-production environments (app.env !== 'production'), the package automatically generates a restrictive robots.txt:
User-agent: *
Disallow: /
This prevents search engines from indexing your development or staging sites.
Production Environment
In production, the package uses your configured rules to generate the robots.txt file.
Advanced Usage
Using the Facade
use Fuelviews\RobotsTxt\Facades\RobotsTxt; // Get robots.txt content $content = RobotsTxt::getContent(); // Generate fresh content (bypasses cache) $content = RobotsTxt::generate(); // Save to a specific disk and path RobotsTxt::saveToFile('s3', 'seo/robots.txt');
Direct Class Usage
use Fuelviews\RobotsTxt\RobotsTxt; $robotsTxt = app(RobotsTxt::class); // Check if regeneration is needed $content = $robotsTxt->getContent(); // Generate and save to custom location $robotsTxt->saveToFile('public', 'custom-robots.txt');
Named Routes
The package registers a named route that you can reference:
// In your views <link rel="robots" href="{{ route('robots') }}"> // Generate URL $robotsUrl = route('robots');
Configuration Options
Disk Configuration
Specify which Laravel filesystem disk to use for storing the robots.txt file:
'disk' => 'public', // or 's3', 'local', etc.
User Agent Rules
Define rules for different user agents:
'user_agents' => [ '*' => [ 'Allow' => ['/'], 'Disallow' => ['/admin', '/dashboard'], ], 'Googlebot' => [ 'Allow' => ['/api/public/*'], 'Disallow' => ['/api/private/*'], ], 'Bingbot' => [ 'Crawl-delay' => ['1'], 'Disallow' => ['/admin'], ], ],
Sitemap Integration
Include sitemap URLs in your robots.txt:
'sitemap' => [ 'sitemap.xml', 'posts-sitemap.xml', 'categories-sitemap.xml', ],
This generates:
Sitemap: https://yoursite.com/sitemap.xml
Sitemap: https://yoursite.com/posts-sitemap.xml
Sitemap: https://yoursite.com/categories-sitemap.xml
Caching System
The package uses an intelligent caching system that regenerates the robots.txt file only when:
- The configuration changes
- The application environment changes
- The application URL changes
- The cached file doesn't exist
Cache Management
Cache is automatically managed, but you can clear it manually:
use Illuminate\Support\Facades\Cache; // Clear the robots.txt cache Cache::forget('robots-txt.checksum');
File Storage
Automatic Storage
The package automatically stores the generated robots.txt file to your configured disk at robots-txt/robots.txt.
Custom Storage
use Fuelviews\RobotsTxt\Facades\RobotsTxt; // Save to specific location RobotsTxt::saveToFile('s3', 'seo/robots.txt'); // Save to multiple locations RobotsTxt::saveToFile('public', 'robots.txt'); RobotsTxt::saveToFile('backup', 'robots-backup.txt');
Example Generated Output
Production Environment
User-agent: *
Allow: /
Disallow: /admin
Disallow: /dashboard
User-agent: Googlebot
Allow: /
Disallow: /admin
Sitemap: https://yoursite.com/sitemap.xml
Sitemap: https://yoursite.com/posts-sitemap.xml
Non-Production Environment
User-agent: *
Disallow: /
Testing
Run the package tests:
composer test
Troubleshooting
Robots.txt Not Updating
If your robots.txt isn't reflecting configuration changes:
- Clear the application cache:
php artisan cache:clear - Ensure your configuration is valid
- Check file permissions for the storage disk
Route Conflicts
If you have an existing /robots.txt route or static file:
- Remove any static
public/robots.txtfile (the package automatically removes it) - Ensure no other routes conflict with
/robots.txt
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
📜 License
The MIT License (MIT). Please see License File for more information.
Built with ❤️ by the Fuelviews team
fuelviews/laravel-robots-txt 适用场景与选型建议
fuelviews/laravel-robots-txt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.22k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 03 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「robots-txt」 「Fuelviews」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 fuelviews/laravel-robots-txt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 fuelviews/laravel-robots-txt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 fuelviews/laravel-robots-txt 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Determine if a page may be crawled from robots.txt and robots meta tags
Manage a virtual robots.txt from the Kirby config file
Laravel init package
Sabhero portfolio package
This is Pico's official robots plugin to add a robots.txt and sitemap.xml to your website. Pico is a stupidly simple, blazing fast, flat file CMS.
A comprehensive painting project estimator package for Laravel applications with Livewire and FilamentPHP
统计信息
- 总下载量: 5.22k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-03-05