chrisrhymes / link-checker
一个用于检查数据库内容中断链的laravel包
Requires
- guzzlehttp/guzzle: ^7.2
Requires (Dev)
- doctrine/dbal: ^3.6
- laravel/legacy-factories: ^1.3
- laravel/pint: ^1.2
- orchestra/testbench: ^8.5
- pestphp/pest: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
README
一个包,它会检查指定模型字段中的断链。它将检查URL字段和包含HTML的字段。
内容
入门
composer require chrisrhymes/link-checker
迁移数据库
php artisan migrate
将Trait添加到您的模型中
将HasBrokenLinks trait添加到您的模型中
<?php namespace App\Models; use ChrisRhymes\LinkChecker\Traits\HasBrokenLinks; use Illuminate\Database\Eloquent\Model; class Post extends Model { use HasFactory, HasBrokenLinks; }
发布配置(可选)
默认情况下,链接检查的超时设置为10秒。也有速率限制的设置。
如果您想更改它,请发布配置文件并更新值。
php artisan vendor:publish --provider="ChrisRhymes\LinkChecker\ServiceProvider"
使用
然后您可以通过检查模型在指定字段中是否有断链来检查。
use ChrisRhymes\LinkChecker\Jobs\CheckModelForBrokenLinks; use ChrisRhymes\LinkChecker\Facades\LinkChecker; $post = Post::first(); // Dispatch the job directly CheckModelForBrokenLinks::dispatch($post, ['content', 'url']); // Or using the facade LinkChecker::checkForBrokenLinks($post, ['content', 'url']);
这将排队一个作业来从模型获取链接,然后它会排队一个作业来检查每个找到的链接。
该作业将在数据库中记录一个具有空URL的断链条目,但会跳过测试mailto或tel链接。
然后您需要运行队列来运行检查。
php artisan queue:work
它使用Laravel Http客户端 Http::get($link)->failed() 来检查链接,如果失败,则确定它为断链。
任何断链都将存储在broken_links表中,与原始模型具有多态关系。
如果抛出异常,例如超时,则还会在broken_links表中记录exception_message。
$post = Post::first(); $post->brokenLinks; // A collection of broken links for the model $post->brokenLinks[0]->broken_link; // The link that is broken $post->brokenLinks[0]->exception_message; // The optional exception message
相对链接
如果您在模型的HTML字段中(不以'http'开头)有相对链接,则可以将第三个参数作为基数传递。CheckModelForBrokenLinks作业将在检查之前将基数附加到相对URL之前。
如果您的相对链接不以/开头,则确保您的基数参数有一个尾随斜杠,'http://example.com/'。
use ChrisRhymes\LinkChecker\Jobs\CheckModelForBrokenLinks; use ChrisRhymes\LinkChecker\Facades\LinkChecker; $post = Post::first(); // Dispatch the job directly CheckModelForBrokenLinks::dispatch($post, ['content', 'url'], 'http://example.com'); // Or using the facade LinkChecker::checkForBrokenLinks($post, ['content', 'url'], 'http://example.com');
速率限制
为了减少同时发送到域的请求数量,此包已启用速率限制。
配置文件允许您设置rate_limit以设置一分钟内可以向单个域名发送多少个请求。默认设置为5,根据您的具体情况调整。
配置文件还允许您设置retry_until,使作业将在时间限制(以分钟为单位)达到之前重试。
用户代理
要为链接检查器发送的请求设置自定义用户代理,请将配置文件中的user_agent设置。例如 'user_agent' => 'my-user-agent',。
默认值为link-checker。
验证SSL
要禁用验证您正在检查的链接的SSL证书,请发布包配置,然后设置'verify' => false,。
这使用HTTP客户端withOptions()来设置Guzzle中的请求选项验证。
测试
测试是用Pest构建的。
使用以下任一命令运行测试。
vendor/bin/pest
// Or
composer test