spatie / laravel-link-checker
Requires
- php: ^7.2|^8.0
- laravel/framework: ~5.8.0|^6.0
- spatie/crawler: ^4.0.3
Requires (Dev)
- orchestra/testbench: ~3.8.0|^4.0
- phpunit/phpunit: ^8.0
README
此包已不再维护
检查Laravel应用中的所有链接
此包提供了一个命令,可以检查您的laravel应用中的所有链接。默认情况下,它将记录所有返回状态码不在200或300范围内的链接。您还可以选择通过邮件发送损坏的链接。
如果您喜欢这个包,请查看我们制作的其他包。
安装
您可以通过composer安装此包
composer require spatie/laravel-link-checker
接下来,您必须安装服务提供者
// config/app.php 'providers' => [ ... Spatie\LinkChecker\LinkCheckerServiceProvider::class, ];
服务提供者将自动注册。
您可以选择使用以下命令发布配置文件:
php artisan vendor:publish --provider="Spatie\LinkChecker\LinkCheckerServiceProvider" --tag="config"
这是已发布配置文件的内容
return [ /* * The base url of your app. Leave this empty to use * the url configured in config/app.php */ 'url' => '', /* * The profile determines which links need to be checked. */ 'default_profile' => Spatie\LinkChecker\CheckAllLinks::class, /* * The reporter determines what needs to be done when the * the crawler has visited a link. */ 'default_reporter' => Spatie\LinkChecker\Reporters\LogBrokenLinks::class, /* * To speed up the checking process we'll fire off requests concurrently. * Here you can change the amount of concurrent requests. */ 'concurrency' => 10 /* * Here you can specify configuration regarding the used reporters */ 'reporters' => [ 'mail' => [ /* * The `from` address to be used by the mail reporter. */ 'from_address' => '', /* * The `to` address to be used by the mail reporter. */ 'to_address' => '', /* * The subject line to be used by the mail reporter. */ 'subject' => '', ], /* * If you wish to exclude status codes from the reporters, * you can select the status codes that you wish to * exclude in the array below like: [200, 302] */ 'exclude_status_codes' => [], ], ];
用法
您可以通过运行此命令开始检查所有链接
php artisan link-checker:run
想在不同url上运行爬虫?没问题!
php artisan link-checker:run --url=https://laravel.net.cn
安排命令
要频繁检查所有链接,您可以安排命令
// app/console/Kernel.php protected function schedule(Schedule $schedule) { ... $schedule->command('link-checker:run')->sundays()->daily(); }
发送损坏的链接邮件
默认情况下,包将记录所有损坏的链接。如果您想通过邮件发送它们,只需在配置文件中的default_reporter
选项中指定Spatie\LinkChecker\Reporters\MailBrokenLinks
即可。
创建您自己的爬取配置文件
爬取配置文件确定需要爬取哪些链接。默认情况下使用Spatie\LinkChecker\CheckAllLinks
,它将检查找到的所有链接。此行为可以通过在配置文件中的default_profile
选项中指定一个类来自定义。该类必须扩展抽象类Spatie\Crawler\CrawlProfile
abstract class CrawlProfile { /** * Determine if the given url should be crawled. * * @param \Psr\Http\Message\UriInterface $url * * @return bool */ abstract public function shouldCrawl(UriInterface $url): bool; }
创建您自己的报告器
报告器确定在爬取链接和爬取过程完成后应该做什么。此包提供了两个报告器:Spatie\LinkChecker\Reporters\LogBrokenLinks
和Spatie\LinkChecker\Reporters\MailBrokenLinks
。您可以通过创建一个扩展抽象类Spatie\Crawler\CrawlObserver
的类来创建自己的行为
abstract class CrawlObserver { /** * Called when the crawler will crawl the url. * * @param \Psr\Http\Message\UriInterface $url */ public function willCrawl(UriInterface $url) { } /** * Called when the crawler has crawled the given url successfully. * * @param \Psr\Http\Message\UriInterface $url * @param \Psr\Http\Message\ResponseInterface $response * @param \Psr\Http\Message\UriInterface|null $foundOnUrl */ abstract public function crawled( UriInterface $url, ResponseInterface $response, ?UriInterface $foundOnUrl = null ); /** * Called when the crawler had a problem crawling the given url. * * @param \Psr\Http\Message\UriInterface $url * @param \GuzzleHttp\Exception\RequestException $requestException * @param \Psr\Http\Message\UriInterface|null $foundOnUrl */ abstract public function crawlFailed( UriInterface $url, RequestException $requestException, ?UriInterface $foundOnUrl = null ); /** * Called when the crawl has ended. */ public function finishedCrawling() { } }
为了更容易创建报告器,您可以通过扩展提供许多有用方法的Spatie\LinkChecker\Reporters\BaseReporter
来创建。
变更日志
请参阅CHANGELOG了解最近更改的更多信息。
测试
首先,在单独的终端会话中启动测试服务器
cd tests/server
./start_server.sh
服务器运行时,您可以执行测试
composer test
贡献
有关详细信息,请参阅CONTRIBUTING。
安全
如果您发现任何与安全相关的问题,请通过电子邮件freek@spatie.be联系,而不是使用问题跟踪器。
Postcardware
您可以自由使用此包(它是MIT许可的),但如果它进入您的生产环境,我们非常欢迎您从家乡寄给我们一张明信片,说明您正在使用我们的哪些包。
我们的地址是:Spatie,Samberstraat 69D,2060 安特卫普,比利时。
所有明信片都将发布在我们网站上。
致谢
支持我们
Spatie是一家位于比利时的安特卫普网络设计公司。您可以在我们网站上找到我们所有开源项目的概述。
您的业务依赖于我们的贡献吗?在Patreon上联系我们并支持我们。所有承诺都将专门用于分配人力维护和新酷炫的东西。
许可
MIT许可(MIT)。有关更多信息,请参阅许可文件。