hissezhaut/laravel-link-checker

检查Laravel应用中的所有链接

v2.0.1 2016-12-05 16:10 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:31:30 UTC


README

Latest Version on Packagist Software License Build Status SensioLabsInsight Quality Score Total Downloads

此包提供了一个命令,可以检查您Laravel应用上的所有链接。默认情况下,它将记录所有未返回200-或300范围内状态码的链接。还有一个选项可以将损坏的链接通过邮件发送。

如果您喜欢这个包,请查看我们制作的其他包

Postcardware

您可以自由使用此包(它是MIT许可),但如果它进入了您的生产环境,您需要从您的家乡给我们寄一张明信片,注明您正在使用我们的哪个包。

我们的地址是:Spatie,Samberstraat 69D,2060 安特卫普,比利时。

最佳明信片将被发布在我们的网站上的开源页面。

安装

您可以通过composer安装此包

composer require spatie/laravel-link-checker

接下来,您必须安装服务提供者

// config/app.php
'providers' => [
    ...
    Spatie\LinkChecker\LinkCheckerServiceProvider::class,
];

您必须注册 \Spatie\LinkChecker\CheckLinksCommand

// app/Console/Kernel.php
protected $commands = [
    ...
    \Spatie\LinkChecker\CheckLinksCommand::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' => '',
        ],
    ],
];

用法

您可以通过发出此命令开始检查所有链接

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();
}

发送损坏的链接邮件

默认情况下,此包将记录所有损坏的链接。如果您希望它们通过邮件发送,只需在配置文件中的 default_reporter 选项中指定 Spatie\LinkChecker\Reporters\MailBrokenLinks

创建自己的爬取配置文件

爬取配置文件决定了哪些链接需要被爬取。默认情况下使用 Spatie\LinkChecker\CheckAllLinks,这将检查它找到的所有链接。此行为可以通过在配置文件中的 default_profile 选项中指定一个类来自定义。该类必须实现 Spatie\Crawler\CrawlProfile 接口。

interface CrawlProfile
{
    /**
     * Determine if the given url should be crawled.
     *
     * @param \Spatie\Crawler\Url $url
     *
     * @return bool
     */
    public function shouldCrawl(Url $url);
}

创建自己的报告器

报告器决定了在爬取链接和爬取过程完成后应该做什么。此包提供了两个报告器:Spatie\LinkChecker\Reporters\LogBrokenLinksSpatie\LinkChecker\Reporters\MailBrokenLinks。您可以通过创建一个类来实现自己的行为,该类必须遵循 Spatie\Crawler\CrawlObserver 接口。

interface CrawlObserver
{
    /**
     * Called when the crawler will crawl the url.
     *
     * @param \Spatie\Crawler\Url $url
     */
    public function willCrawl(Url $url);

    /**
     * Called when the crawler has crawled the given url.
     *
     * @param \Spatie\Crawler\Url                      $url
     * @param \Psr\Http\Message\ResponseInterface|null $response
     */
    public function hasBeenCrawled(Url $url, $response);

    /**
     * Called when the crawl has ended.
     */
    public function finishedCrawling();
}

为了更容易创建报告器,您可以扩展 Spatie\LinkChecker\Reporters\BaseReporter,它提供了许多有用的方法。

变更日志

有关最近更改的更多信息,请参阅变更日志

测试

首先在单独的终端会话中启动测试服务器

cd tests/server
./start_server.sh

服务器运行时,您可以执行测试

composer test

贡献

有关详细信息,请参阅贡献

安全

如果您发现任何安全相关的问题,请通过电子邮件freek@spatie.be而不是使用问题跟踪器。

致谢

关于Spatie

Spatie 是一家位于比利时安特卫普的网页设计公司。您可以在我们的网站上找到我们所有开源项目的概述这里

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件