Blog Detail

27

Sep
Check Broken Links in the HTML of a Laravel Model's Fields cover image

arrow_back Check Broken Links in the HTML of a Laravel Model's Fields

Link Checker is a Laravel package that will check for broken links in an HTML field of a specified model’s fields.

Getting Started

Installation

You can install this package via composer.

composer require chrisrhymes/link-checker

Migrate the database

php artisan migrate

Add the Trait to your models
Add the HasBrokenLinks trait to your model

<?php

namespace App\Models;

use ChrisRhymes\LinkChecker\Traits\HasBrokenLinks;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFactory, HasBrokenLinks;
}

Publish the config (optional)

By default, the timeout for link checks is set to 10 seconds. There are also settings for the rate limiting.

If you wish to change this, publish the configuration file and update the values.

php artisan vendor:publish --provider="ChrisRhymes\LinkChecker\ServiceProvider"

Usage

Then you can check if the model has broken links in the HTML in a specific field.

use ChrisRhymes\LinkChecker\Jobs\CheckModelForBrokenLinks;
use ChrisRhymes\LinkChecker\Facades\LinkChecker;

$post = Post::first();

// Dispatch the job directly
CheckModelForBrokenLinks::dispatch($post, ['content']);

// Or using the facade
LinkChecker::checkForBrokenLinks($post, ['content']);

This will queue a job to get the links from the model, then queue a job to check each link it finds.

You will then need to run the queue to run the checks.

php artisan queue:work

It checks the link using the Laravel Http client Http::get($link)->failed() and if it fails it is determined to be a broken link.

Any broken links will be stored in the broken_links table, with a polymorphic relationship back to the original model.

If an exception is thrown, such as a timeout, then an exception_message will also be recorded in the broken_links table.

$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

Rate Limiting

In order to reduce the number of requests sent to a domain at a time, this package has rate limiting enabled.

The configuration file allows you to set the rate_limit to set how many requests can be sent to a single domain within a minute. The default is set to 5, so adjust as required for your circumstances.

The configuration file also allows you to set the retry_until so the job will be retried until the time limit (in munites) is reached.

For more details, please visit Github.

Published at : 27-09-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project